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 - 


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

Custom validation messages for HTML5 Input elements using the constraint validation API

HTML5 has introduced several input types such as EMAIL, URL, RANGE, SEARCH, DATE, TIME, etc,. Most of the modern browsers have implemented them and are ready to be used in a HTML document. Another exciting feature introduced in HTML5 is the form validation. Instead of writing JavaScript to validate users input, browsers can now validate it and show an appropriate message if the validation fails. The validation message is shown in line with the field for which the validation has failed. The default error message is shown when the validation fails. In this post I'll explain how these error messages can be changed.