CodesJava

Easy learning with example program codes

Backbone.JS Remove() collection

The Backbone.JS Remove() collection method removes a model or array of models from a collection.

Syntax:

Backbone.Collection.Remove ( models, options )   

Parameters:
models: This parameter is used to specify the models to be removed from 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 num3 = new num({number: 4});  
var numCollection = new X();  
numCollection.add([num1,num2,num3]);  
document.write('Number of added numbers : ' + numCollection.length);  
document.write("<br>");  
numCollection.remove([num1]);  
document.write('Number of remaining numbers : ' + numCollection.length);
</script>  
</body>  
</html>

Output:

Number of added numbers : 3
Number of remaining numbers : 2

Explanation:
In the above example, Remove() method removes one model from a collection.

Please follow and like us:
error









Copyright © 2019 CodesJava DMCA.com Protection Status