Introduction
Welcome to the "Demonstrating Backbone.js" article series. This article demonstrates how to use Backbone.js in a sample application. 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:
In this article we will see how to use Backbone.js in a simple store application that displays products.
Here we will use a Google web fonts style sheet and customized style sheet.
google.css
- @font-face {
- font-family: 'PT Sans';
- font-style: normal;
- font-weight: 400;
- src: local('PT Sans'), local('PTSans-Regular'), url(http://themes.googleusercontent.com/static/fonts/ptsans/v5/LKf8nhXsWg5ybwEGXk8UBQ.woff) format('woff');
- }
- @font-face {
- font-family: 'PT Sans';
- font-style: normal;
- font-weight: 700;
- src: local('PT Sans Bold'), local('PTSans-Bold'), url(http://themes.googleusercontent.com/static/fonts/ptsans/v5/0XxGQsSc1g4rdRdjJKZrNBsxEYwM7FgeyaSgU71cLG0.woff) format('woff');
- }
Javascript.aspx
In this app we will display the list of products in a store.
Please check the inline comments for a better understanding.
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="JavaScript.aspx.cs" Inherits="JavaScript.JavaScript" %>
-
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
-
- <!-- Google web fonts -->
- <link href="css/google.css" rel="stylesheet" />
-
- <!-- The main CSS file -->
-
- <link href="css/style.css" rel="stylesheet" />
-
-
- </head>
- <body>
- <form id="main">
- <h1>Store</h1>
-
- <ul id="products">
- <!-- The products will be inserted here -->
- </ul>
-
- <p id="total">total: <span>$0</span></p>
-
- <input type="submit" id="order" value="Order" />
-
- </form>
- <script type="text/javascript" src="backbone/Jquery.js"></script>
- <script type="text/javascript" src="backbone/underscore-min.js"></script>
- <script type="text/javascript" src="backbone/backbone-min.js"></script>
- <script>
- $(function () {
-
-
- var Product = Backbone.Model.extend({
-
-
-
-
- defaults: {
- title: 'My products',
- price: 500,
- checked: false
- },
-
-
- toggle: function () {
- this.set('checked', !this.get('checked'));
- }
- });
-
-
-
- var ProductList = Backbone.Collection.extend({
-
-
- model: Product,
-
-
- getChecked: function () {
- return this.where({ checked: true });
- }
- });
-
-
- var products = new ProductList([
- new Product({ title: 'Iphone', price: 1200 }),
- new Product({ title: 'Xperia', price: 1250 }),
- new Product({ title: 'Galaxy Grand', price: 100 }),
- new Product({ title: 'Google Nexus', price: 500 }),
- new Product({ title: 'Macbook', price: 1500 }),
- new Product({ title: 'Nokia Lumia', price: 1500 })
-
- ]);
-
-
- var ProductView = Backbone.View.extend({
- tagName: 'li',
-
- events: {
- 'click': 'toggleProduct'
- },
-
- initialize: function () {
-
-
-
-
- this.listenTo(this.model, 'change', this.render);
- },
-
- render: function () {
-
-
-
- this.$el.html('<input type="checkbox" value="1" name="' + this.model.get('title') + '" /> ' + this.model.get('title') + '<span>$' + this.model.get('price') + '</span>');
- this.$('input').prop('checked', this.model.get('checked'));
-
-
- return this;
- },
-
- toggleProduct: function () {
- this.model.toggle();
- }
- });
-
-
- var App = Backbone.View.extend({
-
-
- el: $('#main'),
-
- initialize: function () {
-
-
- this.total = $('#total span');
- this.list = $('#products');
-
-
-
-
- this.listenTo(products, 'change', this.render);
-
-
-
-
-
- products.each(function (product) {
-
- var view = new ProductView({ model: product });
- this.list.append(view.render().el);
-
- }, this);
- },
-
- render: function () {
-
-
-
-
- var total = 0;
-
- _.each(products.getChecked(), function (elem) {
- total += elem.get('price');
- });
-
-
- this.total.text('$' + total);
-
- return this;
-
- }
-
- });
-
- new App();
-
- });
- </script>
-
-
-
- </body>
- </html>
Output
Summary
In this article, I explained how to use Backbone.js to make a simple store application. In future articles we will understand more about Backbone.js with examples.