Skip to main content

MVVM in BackboneJS using Backbone.Stickit

I have been looking into various design patterns and trying to architect client side applications using it. More often than not the MVC pattern fits the requirement in most of the applications. However, I have found myself using MVVM (Model View - View Model) pattern along with MVC. I use MVVM pattern particularly when I have to maintain the state of the model on the client side. AngularJS provides great support by allowing you to extend the markup and tying the model right within the view (the markup). It also provides components that can be used to structure the application the MVC way and hence it's appropriately termed as MVW or MV* framework. However, I use BackboneJS in most of my applications and I have been able to maintain the state of the model using the Backbone.Stickit plugin.

When working with Backbone applications, the View's sole responsibility would be to render the model data and also re-render the parts of the view when the state of the model changes. The View listens to the model changes and executes a callback function when the state of the model has changed:

Here in the Backbone View's initialize method, the View is listening to the change in one the model's attribute and it executes the 'updateView' method whenever the 'modelAttribute' is updated. The 'updateView' function then refers to one of the nodes in the View and updates it with the value in 'modelAttribute'. This pattern can be repeated for various attributes in the model and the view can have several functions whose only responsibility is to update a DOM node in the View.

Similarly, when the user inputs data in form elements the same should be reflected in the model. To do that the View will have to declare event handlers:

This has to be repeated for other View elements and developer will be required to write boilerplate code. An alternative to this is to use a Backbone plugin called 'Stickit'. Backbone.Stickit abstracts the above functionality and provides a more cleaner way to bind the View's elements to the model's attributes. In a Backbone View you can define the binding between the model's attributes with the elements in the markup using the 'bindings' object:

Here in the bindings object, the keys are the View elements and the attributes in the model are specified as values. For example, the view element '.js-full-name' is bound to the model attribute 'fullName'. The bindings object provides a two way binding between the View elements and Model attributes. This means that whenever the user inputs some value in the form elements the Model attributes are updated and whenever the Model attribute is updated by some part of the application or by a service call the View elements are updated.

Once the View has compiled the client-side template and rendered it, the View elements should be bound to the Model's attributes. To do that invoke the stickit method on the view object (view.stickit()). I generally use the postRender method and invoke it after the render method has been executed. As the name suggests, the postRender method would execute a set of actions after rendering the template. In this case, call stickit on the view object - this.stickit(); where this refers to the current view.

The Stickit plugin provides several options to configure the bindings. I'll explain those in my next post.


Popular posts from this blog

Adding beforeRender and afterRender functions to a Backbone View

I was working on a Backbone application that updated the DOM when a response was received from the server. In a Backbone View, the initialize method would perform some operations and then call the render method to update the view. This worked fine, however there was scenario where in I wanted to perform some tasks before and after rendering the view. This can be considered as firing an event before and after the function had completed its execution. I found a very simple way to do this with Underscore's wrap method.

De-obfuscating javascript code in Chrome Developer Tools

I had blogged about JavaScript debugging with Chrome Developer Tools  some time back, wherein I have explained how these developer tools can help in debugging javascript code. Today Google Chrome 12 was released and my Chrome browser was updated to this version. As with every release, there have been some improvements made on performance, usability etc,. One feature that stood out for me is the ability to De-obfuscate the javascript code. What is Minification? Minification is the process of removing unnecessary characters such as white spaces, comments, new lines from the source code. These otherwise would be added to make the code more readable. Minifying the source code helps in reducing the file size and thereby reducing the time taken to download the file. This is the reason why most of the popular javascript libraries such as jQuery are minified. A minified jQuery file is of 31 KB in size where as an uncompressed one is about 229 KB. Unfortunately, debugging minified javascript f

On GraphQL and building an application using React Apollo

When I visualize building an application, I would think of using React and Redux on the front-end which talks to a set of RESTful services built with Node and Hapi (or Express). However, over a period of time, I've realized that this approach does not scale well when you add new features to the front-end. For example, consider a page that displays user information along with courses that a user has enrolled in. At a later point, you decide to add a section that displays popular book titles that one can view and purchase. If every entity is considered as a microservice then to get data from three different microservices would require three http  requests to be sent by the front-end app. The performance of the app would degrade with the increase in the number of http requests. I read about GraphQL and knew that it is an ideal way of building an app and I need not look forward to anything else. The GraphQL layer can be viewed as a facade which sits on top of your RESTful services o