What is Backbone.js?
Backbone is a framework for front-end JavaScript, unlike jQuery focus on easier DOM manipulation and event binding, backbone provide a structure for separating data model and DOM, just like a MVC framework separate model, view and controller. Make heavy JavaScript application easier to develop and maintain.
Why Backbone?
In jQuery, we will write a sequence of chain selector for assign data model to DOM element like:
var article = { author:"Joe", content: "testing" }; $('#article').click(function(event){ $(this).find('.content').text(article.content); });For backbone, it provide the Model class and View class, which is the main two element of backbone.
var Article = Backbone.Model; var article = new Article({ author:"Joe", content: "testing" }); var ArticleView = Backbone.View.extend({ el: $('#article'), initialize: function(){ _.bindAll(this,'updateContent'); this.model.bind('change',this.updateContent); }, events: { "click .content" : "updateContent" }, updateContent: function(){ this.$('.content').text(this.model.get("content")); } }); var articleView = new ArticleView({model:article});OK, it's seen like a completely overwork, that's my first thought when I see a clientside MVC framework too, but we have to dig further to understand why people create it.
Event Binding
First, it separate the data model and view - which include event handle and DOM element, we can easily bind data model in view like:
this.model.bind('change',this.updateContent);It means when the data model change, the view will be automatically updated to DOM element.
Also, backbone provide literal event binding:
events:{ "click .content": "updateContent" }which bind the "click" event to ".content" class, trigger "updateContent" function in current view.
It's useful when binding multiple events.
Restful synchronization
Furthermore, the backbone provide a RESTful format API for synchronize data model with server.
every model class in backbone have a corresponding url for synchronize, and when the "model.save" or "model.fetch" method is called, the model will make an Ajax request to update data with server.
For example:
article.save({author:"John"});will first find article.sync method, if undefined, call Backbone.sync.
The sync method will make an ajax request to server, and update model, trigger event and update to view.
the url of model is defined by RESTful API:
- create -> POST /model
- read -> GET /model[/id]
- update -> PUT /model/id
- delete -> DELETE /model/id
and we can also change it by overwrite url parameter
Dependency
backbone.js depend on underscore.js and jQuery/Zepto library, since it is only a 3.9k framework focus on the structure of JavaScript application.
Collection and Controller
backbone provide Collection, which is the collection of Backbone.Model,the Controller in backbone is more like the route map in MVC framework, it provide clientside fragment url routing, eg. "/#article/12" match "article(id)". So the url can be recorded and bookmarked by browser.
Template
In Backbone, we can create template in view.In that way we can add and remove multiple view and render with template.
The underscore.js provide template engine or we can use other engine too.
In view:
render:function(){ var template = "<%=content %>"; $(this.el).html( this.template(this.model.toJSON()) ); return this; }
Conclusion
JavaScript MVC framework is good for rich client application and also can keep your javascript cleaner.
But whether to use it depend on how much script in your application, for small page, the MVC is total overhead, but it is good for large scale page like Mail or Desktop like application.
More
BackboneSample todo application
Annotated source
On this line,
ReplyDeletevar template = "<div class="content"><%=content %></div>";
Shouldn't you use ' or "" ?
Remember, you can also put your templates into <script> and then select them using jQuery. Then you won't have quote issues either.
Well, I think blogger change single quote to double quote for me.
ReplyDeleteAnd thanks for the advice on template.