Sunday, November 17, 2024
Google search engine
HomeLanguagesJavascriptBackbone.js toJSON Model

Backbone.js toJSON Model

The Backbone.js clone is used to return the attributes of the given object in JSON format. We have to use JSON.stringify() to return the attributes.

Syntax:

Backbone.Model.toJSON(options)

Parameters:

  • options: Used to take the attribute name.

If it is not specified, then it will return the whole model.

Example 1: In this example, we will display all the attributes in a book model.

HTML




<!DOCTYPE html>
<html>
  
<head>
      
    <script src=
            type="text/javascript">
    </script>
    <script src=
        type="text/javascript">
    </script>
    <script src=
        type="text/javascript">
    </script>
      
</head>
  
<body>
    <script type="text/javascript">  
        var Books = Backbone.Model.extend();  
        var book = new Books(
              {
              book_name:"css",
              price:900,
              type:"web"
            });  
        document.write("Values in book model:  ", 
                       JSON.stringify(book));        
              
    </script
</body>
</html>


Output:

Values in book model:
{
    "book_name":"css",
    "price":900,
    "type":"web"
}

Example 2: The following code demonstrates the toJSON model with an empty object.

HTML




<!DOCTYPE html>
<html>
  
<head>
      
    <script src=
            type="text/javascript">
    </script>
    <script src=
        type="text/javascript">
    </script>
    <script src=
        type="text/javascript">
    </script>
      
</head>
  
<body>
    <script type="text/javascript">  
        var Books = Backbone.Model.extend();  
        var book = new Books();  
        document.write("Values in book model:  ", 
                       JSON.stringify(book));      
              
    </script
</body>
</html>


Output:

Values in book model: {}

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments