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.

Comments

Popular posts from this blog

How to use the APP_INITIALIZER token to hook into the Angular bootstrap process

I've been building applications using Angular as a framework of choice for more than a year and this post is not about another React vs Angular or the quirks of each framework. Honestly, I like Angular and every day I discover something new which makes development easier and makes me look like a guy who built something very complex in a matter of hours which would've taken a long time to put the correct architecture in place if I had chosen a different framework. The first thing that I learned in Angular is the use of the APP_INITIALIZER token.

Using MobX to manage application state in a React application

I have been writing applications using React and Redux for quite some time now and thought of trying other state management solutions out there. It's not that I have faced any issues with Redux; however, I wanted to explore other approaches to state management. I recently came across MobX  and thought of giving it a try. The library uses the premise of  `Observables` to tie the application state with the view layer (React). It's also an implementation of the Flux pattern wherein it uses multiple stores to save the application state; each store referring to a particular entity. Redux, on the other hand, uses a single store with top-level state variables referring to various entities.

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...