CodesJava

Easy learning with example program codes

Backbone.JS Models() collection

The Backbone.JS Models() collection method is used to specify the array of models which are created inside a collection.

Syntax:

Collection.Models   

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 name1 = new X({  
name: "Jim",  
age: 10
});  
var name2 = new X({  
name: "Mini",  
age: 15
});  
var Z = new Y([name1,name2]);   
document.write("Collection: "+JSON.stringify(Z.models));  
</script>  
</body>  
</html>

Output:

Collection: [{"name":"Jim","age":10},{"name":"Mini","age":15}]

Explanation:
In the above example, the model ‘X’ includes the default values and is extended using the Backbone.Model class, ‘Y’ is a collection instance and The ‘Z.models’ define the array of models inside the collection.

Please follow and like us:
error









Copyright © 2019 CodesJava DMCA.com Protection Status