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/
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
Post a Comment