Backbone JS (Stop tying your data to DOM)
Backbone.js is not a full fledged mvc or mvp or mvvm framework, Basically it provides the event bindings between the model and view, so that changes in the view will update the attributes of model and corresponding views listening to the model will get updated.
But there are many backbone js plugins, which help us to create a web application using backbone depending on the requirement. So we can implement mvc, mvp, mvvm or anything using these plugins.
What is a backbone model ? (State)
Backbone model maintains state of the application. Following things can be done in the model.
Responsibilities:
1)Getting the data from server
2)Save data to the server
3)Validation of data
4)Syncing of the data from server to client
5)Destroying the model (data,if no longer needed )
Usage:
A model has attributes to store the data, on change of any of the attribute a change event will be triggered by the model
However, this change event can also be suppressed by passing {silent:true}. views should listen to this events and display the data
Gotchas:
1)If the attribute is again a model, then the change in the attribute of the submodel will not be triggered in parent model. Use the deepModelbinderplugin.
2)Even while instantiating a model with an object, the attributes will be set and validation if exist on any attribute will trigger.
3)Becareful in specifying the initial values, if initial value is not set, then the value is treated as undefined
What is a backbone collection?
A collection is just a list of models.
Responsibilities:
1)Fetch data from server
2)Sync data to the server
Usage:
On addition or removal of model to a collection the change event will be triggered. Views should listen to this events and display the data
Gotchas:
1)While creating the collection from a model, if the validate method fails then the model will not get added into the collection
What is a backbone view ?
A view is basically UI component that displays the data to the user intutively. it interacts with the template for markup
Responsibilities:
1)listen to the model and display data when data is changed.
Gotchas:
1)Beware of zombie views, they should be destroyed and all their events should be undelegated otherwise they will become zombies
What is a backbone router?
A router allows us to configure url corresponding to the view. It should be used only to provide views as a bookmarkable url. Don’t use it as a controller.
Responsibilties:
1)It provides route and callback method corresponding to the creation of view
Usage:
Use router.navigate or Backbone.history.navigate to route to a different url. Pass {trigger:true} to trigger the callback method corresponding to the view
Gotachs:
1)By default callback corresponding to the empty route will be called.
2)This is not a controller and never should be used a controller. it should not be passed to the views. use Backbone.history.navigate() method for such cases.
P.S: This post is for developers, who are new to backbone. Shortly, Continue Reading »