Introduction
Welcome to the "Demonstrating Backbone.js" article series. This article demonstrates how to create and use collections in Backbone.js. This article starts with the concept of Backbone.js and various components of it. Previous articles have provided an introduction to views and the implementation of routers and collections. You can get them from the following:
- Demonstrating Backbone.js: Implement View
- Demonstrating Backbone.js: Implement View Part 1
- Demonstrating Backbone.js: Implement View Part 2
- Demonstrating Backbone.js: Implement View Part 3
- Demonstrating Backbone.js: Implement View Part 4
- Demonstrating Backbone.js :Implement View Part 5
- Demonstrating Backbone.js :Implement Routers Part 1
- Demonstrating Backbone.js :Implement Collections
In this article we will see a few more concepts of collections in Backbone.js.
Push Model Object to Collection
Here we have “Product” as a model property of a “ProductCollection” collection and we have added a p1 and p2 object to the “ProductCollection” collection using the add() method and we are displaying the length of the collection.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="JavaScript.aspx.cs" Inherits="JavaScript.JavaScript" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<script src="backbone/Jquery.js" type="text/javascript"></script>
<script src="backbone/underscore-min.js" type="text/javascript"></script>
<script src="backbone/backbone-min.js" type="text/javascript"></script>
</head>
<body>
<script type="text/javascript">
var Product = Backbone.Model.extend({
defaults: {
name: 'Iphone',
manufacturer: 'Apple'
}
});
var ProductCollection = Backbone.Collection.extend({
model: Product
});
//create object of Product class
var product1 = new Product({});
var product2 = new Product({});



Comments
Join the conversation! Your thoughts help the community grow.