Skip to main content

Client side MVC with backbonejs - a simple example

Some developers do not pay much attention about the structure of the client side code in their web applications, and the way they organize the code may end up in a tangled pile of JavaScript codes which is less readable and difficult to maintain. Backbonejs is a framework for web front ends which solve these issue. You can build web applications with rich front ends with backbonjs and connects those all to your existing APIs over RESTful JSON interfaces.

In this post, I am trying to demonstrate how to build a client side MVC application with backbonejs, as simple as possible. The application is a list, which you can add and remove items. This is a very simplified version of the TodoMVC at http://todomvc.com, please proceed to the references at the end for more details.

Source code of this example : https://github.com/nadeeth/backbonejs_simple_example 

Application includes a model for individual list item, a collection to store list items, views/controllers to display, create, and delete items.

1. Index File

First we create an index file to hold all the html needed for the application, and inside this file we can load the libraries, basic application dependencies and application specific files.

<!doctype html>
<html lang="en" data-framework="backbonejs">
    <head>
        <title>Backbonejs Simple MVC Example</title>
    </head>
    <body>

        <!-- The List -->
        <section id="list-section">
            <header id="header">
                <h1>The List</h1>
                <input id="new-item" placeholder="Type Here" autofocus>
            </header>
            <section id="main">
                <ul id="items-list"></ul>
            </section>
        </section>

        <!-- The micro template for a single item -->
        <script type="text/template" id="item-template">
            <div class="view">
                <label><%- title %></label>
                <a href="#" class="destroy">X</a>
            </div>
        </script>

        <!-- Libraries -->
        <script src="lib/jquery.js"></script>
        <script src="lib/underscore.js"></script>
        <script src="lib/backbone.js"></script>
        <script src="lib/backbone.localStorage.js"></script>
        
        <!-- MVC -->
        <script src="js/models/item.js"></script>
        <script src="js/collections/list.js"></script>
        <script src="js/views/item-view.js"></script>
        <script src="js/views/list-view.js"></script>
        <script src="js/app.js"></script>
    </body>
</html>

First section inside the body is the markups for the list which includes the heading, a text box to type list titles, and the empty list. When you type something in the text box and press enter the text should go to the list as an Item.

Next is the micro template for a single list item, in this application, template utility of underscorejs has been used to handle the templates. But you can use other JavaScript templating systems as well (Ex: handlebars).

Next section includes underscore frame work and other libraries (jQuery, underscorejs, and backbone local storage). Here, underscorejs is the only hard dependency of backbonejs.

Last section includes the application specific codes which we are going to discuss in the rest of this post.

2. Model (js/models/item.js)

/*global Backbone */
var app = app || {};

(function () {
 'use strict';

 app.Item = Backbone.Model.extend({

  // Default attributes
  defaults: {
   title: ''
  }
 });
})();

This is the model for a single list item, with the only default attribute "title".

3. Collection (js/collections/list.js)

/*global Backbone */
var app = app || {};

(function () {
    'use strict';

    var List = Backbone.Collection.extend({

        // Reference to this collection's model
        model: app.Item,

        // Save all the items under the `"items"` namespace
        localStorage: new Backbone.LocalStorage('items-backbone'),

        // Next order number for new items
        nextOrder: function () {
            if (!this.length) {
                return 1;
            }
            return this.last().get('order') + 1;
        },

        // Items are sorted by their original insertion order
        comparator: function (item) {
            return item.get('order');
        }
    });

    // Create our global collection of **Items**.
    app.list = new List();
})();

This collection groups the list items. Here we define the local storage namespace to store data, a function to get next order when creating a new item, and a comparator function to sort items.

4. List View (js/views/list-view.js)

var app = app || {};

(function ($) {
    'use strict';

    app.AppView = Backbone.View.extend({

        el: '#list-section',

        // Create New Items
        events: {
            'keypress #new-item': 'createOnEnter'
        },

        // Bind to the relevant events at intialization 
        initialize: function () {

            this.$input = this.$('#new-item');
            this.$list = $('#items-list');

            this.listenTo(app.list, 'add', this.addOne);
            this.listenTo(app.list, 'reset', this.addAll);

            //{reset: true} - To suppresses 'add' events for intial loading  
            app.list.fetch({reset: true});
        },

        render: function () {

        },

        // Add a single list item
        addOne: function (item) {
            var view = new app.ItemView({ model: item });
            this.$list.append(view.render().el);
        },

        // Add all items in the list
        addAll: function () {
            this.$list.html('');
            app.list.each(this.addOne, this);
        },

        // Get the list of attributes for a new item
        getAttributes: function () {
            return {
                title: this.$input.val().trim(),
                order: app.list.nextOrder()
            };
        },

        // Create a new item when pressing the enter key in text box
        createOnEnter: function (e) {

            if (e.which !== ENTER_KEY || !this.$input.val().trim()) {
                return;
            }

            app.list.create(this.getAttributes());
            this.$input.val('');
        }

    });
})(jQuery);

In backbonejs, the view is the real controller, and the actual views are the html templates. Views, and templates together performs the job of controllers and views as in server side MVC application. There for, main application logic goes inside these views.

Most important thing to notice here is to how the view handles the events and listen to the changes of data and models. The function "createOnEnter" is called when the key down event occurs, and the view listens to the "add" event to update the list by calling "addOne" function when a new model(item) is added to the collection(list).

5. Item View (js/views/item-view.js)

var app = app || {};

(function ($) {
    'use strict';

    app.ItemView = Backbone.View.extend({

        // Tag name for the current view
        tagName:  'li',

        // Template for a single item
        template: _.template($('#item-template').html()),

        // Events specific to an item
        events: {
            'click .destroy': 'clear'
        },

        // Listens for changes to item model
        initialize: function () {
            this.listenTo(this.model, 'destroy', this.remove);
        },

        // Render the item
        render: function () {

            this.$el.html(this.template(this.model.toJSON()));
            return this;
        },

        // Remove the item from storage and UI
        clear: function () {
            this.model.destroy();
        }
    });
})(jQuery); 

This is the view for a single list item. When rendering, it uses the micro template and the data from a model to render the new list item. When the click event occurs to delete an item, the "clear" function is called to remove the item from both UI and Storage.

6. App.js (js/app.js)

var app = app || {};
var ENTER_KEY = 13;
var ESC_KEY = 27;

$(function () {
    'use strict';

    new app.AppView();
});

Finally, the list view should be instantiated when the page is loaded. In addition to that, some global variables we need in the application have been defined here.

Please go through the references given below for leaning backbonejs in detail.

[References]
http://backbonejs.org
Developing Backbone.js Applications by Addy Osmani
Backbonejs TodoMVC

Comments

Popular posts from this blog

How to create a new module for vtiger...

Recently, I had to create a new module for vtigerCRM for my client in current working place. I did search in many places including the official vtiger sites, but couldn’t find a better documentation for my purpose. The latest vtiger version at that time was 5.0.3. Because I had some experience doing lots of core modifications for this system, I did decide to read the source code and find how to add a new module. Finally, I could create a new module and started the project. So, I thought it will be a good thing to write some thing on my blog about this topic, so that others who want to do this thing can read. Given below is a brief description about how to create a new module for vtiger CRM 5.0.3. Source code of this example module is also available to Download.
Step 01: Creating the module directory and minimum required files.
Create a directory called “newModule” inside your vtiger modules directory, or any other name that you prefer. Now, module index file should be created. Create a…

De Morgan's Laws in Programming

Recently, while I was reviewing some codes, I saw there were some conditional statements that check for the same condition but written in different ways. Most of these statements were written with common sense without using any mathematical analysis, since those are too simple to go for a more formal approach. The two identical conditional statements that has been written in different ways are given below.

01)

if ($comment['deleted'] == '1' || $comment['approved'] == '0') {
                unset($conversationsArray[$key]);
} else {
               ++$count;
}

02)

if ($comment['deleted'] == '0' && $comment['approved'] == '1') {
               ++$count;             
} else {
             unset($conversationsArray[$key]);
}

Obviously, the above lines say that the inverse of the first condition is equals to the second condition and vice versa. That is...

 ($comment['deleted'] == '1' || $comment['ap…

How to create a simple Web Crawler

Web crawlers are used to extract information from web sites for many purposes.

The simple example given here accepts an URL and exposes some functions to query the content of the page.

To check out the source code of this example : https://github.com/nadeeth/crawler

If you are going to make any improvements to this code, I recommend you to follow TDD and use the unit test class in the code.

Step 1 : Create the class, init function and required attributes In this example, xpath is used for querying the given web page. There is an attribute to hold the page url, and another to hold the xpath object of the loaded page.

The init() function initializes the xpath object for the page URL assigned to url attribute.

class Crawler { public $url = false; protected $xpath = false; public function init() { $xmlDoc = new DOMDocument(); @$xmlDoc->loadHTML(file_get_contents($this->url)); $this->xpath = new DOMXPath(@$xmlDoc); } } In the next two ste…