Backbone.js unshift Collection
The Backbone.js unshift Collection is used to add model at the beginning of a Collection. It takes model as the first parameter and options as the second parameter. In this article, we will discuss the Backbone.js unshift Collection.
Syntax:
collection. unshift( model, options );
Parameters:
- model: This parameter is used to specify the model’s instance which is going to add to the collection.
- options: This is an optional parameter regarding the model which is being added to the collection.
Example 1: In this example, we will illustrate The Backbone.js unshift collection. In this example, we will add 2 models using unshift in the collection.
HTML
<!DOCTYPE html> < html > < head > < title >BackboneJS unshift collection</ 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.2.2/backbone-min.js" type = "text/javascript" > </ script > </ head > < body > < h1 style = "color: green;" > w3wiki </ h1 > < h3 >BackboneJS unshift collection</ h3 > < script type = "text/javascript" > var Book = Backbone.Model.extend(); var books = Backbone.Collection.extend({ model: Book }); var Library = new books(); var b1 = new Book({ title: "catch-22", Author: "Joshep Heller" }); var b2 = new Book({ title: "Invisible Man", Author: "Ralph Ellison" }) Library.unshift(b1); Library.unshift(b2); document.write("Values : < br > "); document.write(JSON.stringify(Library)); </ script > </ body > </ html > |
Output:
Example 2: In this example, we will again add two models at beginning of the collection.
HTML
<!DOCTYPE html> < html > < head > < title >BackboneJS unshift collection</ 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.2.2/backbone-min.js" type = "text/javascript" > </ script > </ head > < body > < h1 style = "color: green;" > w3wiki </ h1 > < h3 >BackboneJS unshift collection</ h3 > < script type = "text/javascript" > var Book = Backbone.Model.extend(); var books = Backbone.Collection.extend({ model: Book }); var Library = new books(); var b1 = new Book({ title: "catch-22", Author: "Joshep Heller" }); var b2 = new Book({ title: "Invisible Man", Author: "Ralph Ellison" }) Library.unshift(b1); Library.unshift(b2); document.write("Values : < br > "); document.write(JSON.stringify(Library)); document.write("< br >< br >"); var b3 = new Book({ title: "Wings of Fire", Author: "A. P. J. Abdul Kalam" }); var b4 = new Book({ title: "The Palace of Illusion", Author: "Chitra Banerjee" }); Library.unshift(b3); Library.unshift(b4); document.write("Values again unshifted : < br > "); document.write(JSON.stringify(Library)) </ script > </ body > </ html > |
Output:
Reference: https://backbonejs.org/#Collection-unshift
Contact Us