CodesJava

Easy learning with example program codes

Backbone.JS Shift() collection


The Backbone.JS Shift() collection method removes the first item from the collection.

Syntax:

Collection.Shift ( options )   

Parameters:
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 Students = Backbone.Model.extend({  
defaults: {  
name: 'Tom',  
age: 10  
}  
});  
var X = Backbone.Collection.extend({  
model: Students  
});  
var Student1 = new Students({ id: 100, name: 'Jim', age: 11}); 
var Student2 = new Students({ id: 200, name: 'Tim', age: 9}); 
var studarray = [Student1, Student2];  
document.write("<b>Original array:</b>",JSON.stringify(studarray));  
studarray.shift();  
var Y = new X(studarray);  
document.write("<br><b>After Shift:</b>",JSON.stringify(Y)); 
</script>  
</body>  
</html>

Output:

Original array:[{"id":100,"name":"Jim","age":11},{"id":200,"name":"Tim","age":9}]
After Shift:[{"id":200,"name":"Tim","age":9}]

Explanation:
In the above example, the Shift() method removes the model Student1 from the collection.

Please follow and like us:


Copyright © 2019 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki