Google News
logo
Backbone - Interview Questions
What is Collection in Backbone.js?
A Collection can be defined as an ordered set of modules. In Backbone.js, there is a collection class which provides some useful methods to deal with the collections. We can extend the collection class to provide some additional functionalities. For example :
<script type="text/javascript">    
        //The model 'MyTeam' includes default values and  extended using the Backbone.Model class    
         var MyTeam = Backbone.Model.extend({    
            defaults: {    
               player: "Dhyanchand",    
               country: "India"    
            },    
         });    
    
         //'MyTeam1' is an instance of the collection    
         var MyTeam1 = Backbone.Collection.extend({    
            model: MyTeam  //model 'MyTeam' is specified for a collection by overriding the 'model' property    
         });    
    
         //The collection 'MyTeam1' is instantiated by using new keyword    
         var myval=new MyTeam1({});    
    
         //The JSON.stringify() method returns values of collection in the JSON format    
         document.write("The values in the collection are: ",JSON.stringify(myval));    
 </script>

 

Advertisement