CodesJava

Easy learning with example program codes

Backbone.JS Add() collection

The Backbone.JS Add() collection method adds a model or array of models to a collection.

Syntax:

Backbone.Collection.Add ( models, options )   

Parameters:
models: This parameter is used to specify the models to be added 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.add([num1,num2]);  
document.write('Number of added numbers : ' + numCollection.length);  
</script>  
</body>  
</html>

Output:

Number of added numbers : 2

Explanation:
In the above example, the Add() method adds two models to an already existing collection.

Please follow and like us:
error









Copyright © 2019 CodesJava DMCA.com Protection Status