Backbone.js unshift Collection
Last Updated :
12 Dec, 2022
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 >
type = "text/javascript" >
</ script >
< script src =
type = "text/javascript" >
</ script >
< script src =
type = "text/javascript" >
</ script >
</ head >
< body >
< h1 style = "color: green;" >
GeeksforGeeks
</ 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:
Backbone.js unshift collection
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 >
type = "text/javascript" >
</ script >
< script src =
type = "text/javascript" >
</ script >
< script src =
type = "text/javascript" >
</ script >
</ head >
< body >
< h1 style = "color: green;" >
GeeksforGeeks
</ 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:
Backbone.js unshift collection
Reference: https://backbonejs.org/#Collection-unshift
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...