Open In App

Backbone.js toJSON Collection

In this article, we will discuss Backbone.js toJSON collection. The Backbone.js toJSON collection is used to extend the backbone’s collection class in which we can create our own collection.

Syntax:



Backbone.Collection.toJSON(options);

Parameters: It accepts one parameter that is described below:

Example 1: In this example, we will create an array model with one value and return it in JSON format.






<!DOCTYPE html>
<html>
  
<head>
            type="text/javascript"></script>
    <script src=
        type="text/javascript"></script>
    <script src=
        type="text/javascript"></script>
    <script type="text/javascript">
  
        var data = Backbone.Model.extend({
            defaults: {
                id: "1",
                name: "sravan kumar gottumukkala"
            },
        });
  
        var data1 = Backbone.Collection.extend({
            model: data
        });
  
        var final = new data1({});
  
        document.write("Values:", 
                       JSON.stringify(final.toJSON()));   
    </script>
</head>
  
<body></body>
  
</html>

Output:

Values:[{"id":"1","name":"sravan kumar gottumukkala"}]

Example 2: In this example, we will create another array model with one value and return it in JSON format.




<!DOCTYPE html>
<html>
  
<head>
            type="text/javascript"></script>
    <script src=
        type="text/javascript"></script>
    <script src=
        type="text/javascript"></script>
    <script type="text/javascript">
  
        var data = Backbone.Model.extend({
            defaults: {
                id: "1",
                name: "sravan kumar gottumukkala",
                age: "67"
            },
        });
  
        var data1 = Backbone.Collection.extend({
            model: data
        });
  
        var final = new data1({});
  
        document.write("Values:", 
                       JSON.stringify(final.toJSON()));   
    </script>
</head>
  
<body></body>
  
</html>

Output:

Values:[{"id":"1","name":"sravan kumar gottumukkala","age":"67"}]

Reference: https://backbonejs.org/#Collection-toJSON


Article Tags :