- for what kind of project
So letA's start with Backbone. The documentation of Backbone is available under the following link. The complete documentation is written on a single page. Its very easy to search for topics you are interested in. After a short introduction youA'll find one or more code examples for each topic. These examples are nicely short and well described. Some of them are testable right away. Everything is easy to understand. The only thing missing is a short demonstration how all parts of Backbone work together. But if you search for Backbone snippets on Google you will find lots of examples because it has a wide acceptance among frontend developers.
Backbone.Model It wraps a row of data into business logic. The backbone.Model provides a basic set of functionality for managing changes. An easy model could look like this:
In this piece of code we extend Backbone.Model with our domain-specific method A"promptNameA". The A"this.setA" function sets a hash of attributes on the model and triggers a A"changeA" event. ItA's one of the base functions in Backbone.Model and really handy for updating your view if something changed. You easily bind a A"change" listener to the instance of your model to observe any changes. Within the callback of your binding you can place your view changes or whatever you like. LetA's have a look at an example.
Backbone.View The general idea of Backbone.View is to organize the interface to logical views. Backed by models each view can be updated independently when the model changes. As well as the model, the view comes along with basic methods like (initialize, render, remove, ...).
In the example we overwrite the function A"initialize" which is the constructor of the view class. It is always invoked if an instance is created. Within this constructor we bind the A"change:nameA" listener to the instance of the PersonModel and bind a callback. This callback would be invoked if the name in the model changes. If this happens the new name would be rendered between the body tags.
Backbone.Collection Backbone.Collections are ordered sets of models. Like the model you can bind A"changeA" events as well, so the collection get notified if any model changes. It provides a full suite of underscore.js methods to improve the collection handling.
Backbone.Router Backbone.Router maps URLs to functions and connects them to actions and events. For history routing it uses the history API. So you can use standard URLs like A"/pageA" instead of A"#pageA". Backbone provides a graceful fallback for browsers without support of the history API.
To compare the complexity of each library I built a simple wish list application. This application should be able to add wishes, remove them and count all added wishes. An implementation with Backbone could look like this:
Truncated by Planet PHP, read more at the original (another 10790 bytes)