Backbone.JS Extend() collection

The Backbone.JS Extend() collection method extends the backbone’s collection class to create own collection.

Syntax:

Backbone.Collection.extend ( properties, classProperties )   

Parameters: properties: This parameter is used to specify the instance properties of a collection. classProperties: This parameter is used to specify the class properties of a collection.

Example:

<!DOCTYPE html>  
<html>
<head>  
<title>Example</title>  
<script src="https://code.jquery.com/jquery-2.1.3.min.js" type="text/javascript"></script>  
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js" 
type="text/javascript"></script>  
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js" 
type="text/javascript"></script>  
</head>  
<body>  
<script type="text/javascript">  
var X = Backbone.Model.extend({  
defaults: {  
name: "Tom",  
age: 20
},  });  
var Y = Backbone.Collection.extend({  
model: X  
});  
var Z=new Y({});  
document.write("Collection: ",JSON.stringify(Z));  
</script>  
</body>  
</html>

Output:

Collection: [{"name":"Tom","age":20}]

Explanation: In the above example, the model ‘X’ includes the default values and is extended using the Backbone.Model class, and ‘Y’ is an instance of the collection.

Please follow and like us:
Content Protection by DMCA.com