CodesJava

Easy learning with example program codes

Backbone.JS Push() collection

The Backbone.JS Push() collection method push the models to a collection, which simply means that it adds a model to the end of a collection.

Syntax:

Backbone.Collection.Push ( models, options )   

Parameters:
models: This parameter is used to specify the models to be Pushed to the collection.
options: This parameter is used to specify the model types.

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 num = Backbone.Model.extend({  
defaults: {  
number: 1   
}  });   
var X = Backbone.Collection.extend({  
model: num  
});  
var num1 = new num({number: 2});  
var num2 = new num({number: 3});  
var numCollection = new X(); 
numCollection.push(num1);  
numCollection.push(num2);    
document.write('Number of numbers added: ' + numCollection.length); 
</script>  
</body>  
</html>

Output:

Number of numbers added: 2

Explanation:
In the above example, the Push() method adds the two models to the collection.

Please follow and like us:
error









Copyright © 2019 CodesJava DMCA.com Protection Status