Skip to main content

More on MVVM with Backbone Stickit plugin

In my last post, I mentioned about two way binding a.k.a MVVM in Backbone using Backbone.Stickit plugin. The Stickit plugin does provide awesome two way binding, but in addition it includes several features that are very much apt when modifying state of model or the view. I've been using these features in my project regularly.

The bindings configuration is used to specify the binding between the view and model. When you specify this configuration, the model changes are observed and view is updated whenever there is a change and similarly any changes in the view would update the model. There are many scenarios where in the format of the model or view has to be changed. For example, the model stores a date in a specific format and the view has to display the same in a different format. The Stickit plugin provides methods - onGet and onSet which can be used in such scenarios:



Here, instead of providing a one to one mapping between the view element and the model attribute, an object containing the binding configuration is specified. The configuration has three attributes - observe, onGet and onSet. The 'observe' attribute specifies the model attribute that the view element should be mapped to. Before the view element is updated, the 'onGet' function is called, here the function formats the date value present in the model and the view is updated with the formatted value. Now when you change the date in the view, the 'onSet' method is invoked before updating the model. The 'onSet' method, formats the input date and updates the model. These two methods intercept the update of view and model; helping you to format data correctly.

The other feature that I use regularly when working with authorization piece is the use of 'visible' and 'visibleFn'. In a scenario where, based on the logged in user's credentials some part of the page would be hidden or shown to the user; this feature comes very handy.


Here the function assigned to 'visible' attribute is executed to check whether the logged in user is an 'admin'. It returns a boolean value and based on the whether it is true or false, the view element would be shown or hidden. The 'visibleFn' is optional, by default if 'visible' is true then the view element would have the style attribute 'display' set to 'block' and if false, the element would have 'display' property set to 'none'. If you want change the 'display' property from 'block' to say 'inline-block' or if you want perform an animation before hiding the element, then the 'visibleFn' should be specified.

Another feature that I have used in my projects is the use of 'selectOptions'. This feature is used to populate a 'select' element in the form with 'option' tags using a collection of objects.

Here 'selectOptions' specifies the collection attribute which contains an array of objects, each object with properties 'timeZoneName' and 'timeZoneId'. These properties are assigned to 'labelPath' and 'valuePath' attributes. When 'option' tags are generated, the label name and value of the option tag would be referencing these properties. The 'defaultOption' configuration is used to either specify the default value or define a placeholder for the select element. When you select an element from the 'select' drop down, the model attribute 'timezone' would be updated.

There are several other features in the Stickit plugin, like adding custom handlers, selectively updating the model or view - http://nytimes.github.io/backbone.stickit/ 

Comments

Popular posts from this blog

File upload and Progress events with HTML5 XmlHttpRequest Level 2

The XmlHttpRequest Level 2 specification adds several enhancements to the XmlHttpRequest object. Last week I had blogged about cross-origin-requests and how it is different from Flash\Silverlight's approach .  With Level 2 specification one can upload the file to the server by passing the file object to the send method. In this post I'll try to explore uploading file using XmlHttpRequest 2 in conjunction with the progress events. I'll also provide a description on the new HTML5 tag -  progress which can be updated while the file is being uploaded to the server. And of course, some ColdFusion code that will show how the file is accepted and stored on the server directory.

Server sent events with HTML5 and ColdFusion

There are several ways to interact with the server apart from the traditional request\response and refresh all protocol. They are polling, long polling, Ajax and Websockets ( pusherapp ). Of all these Ajax and Websockets have been very popular. There is another way to interact with the server such that the server can send notifications to the client using Server Sent Events (SSE) . SSE is a part of HTML5 spec:  http://dev.w3.org/html5/eventsource/

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.