Introduction
What Backbone is:
What Backbone isn't:
*)As I understand it.
Typical init-function (run onDomReady):
// Create a new instance of MovieCollection.
var movies = new MovieCollection();
// Populate the collection with data from the server.
movies.fetch();
// Instantiate a FilterModel.
var filter = new FilterModel();
// Instantiate a FilterView with the FilterModel as
// backing model and assign it the #filter element to render in.
var filterView = new FilterView({model: filter, el: "#filter"});
// Instantiate a MovieListView with the MovieCollection as
// backing collection and assign it the #list element to render in.
var movieListView = new MovieListView(
{collection: movies, filter: filter, el: "#list"});
// Render the views.
filterView.render(); movieListView.render();
dom
Decouple views by communicating via events:
var SimpleModel = Backbone.Model.extend( {
defaults: function () { return { "name": "Nothing here yet" }; }
});
var ManipulatorView = Backbone.View.extend({
events: { "keyup": "updateModelName" },
render: function () { return this; },
updateModelName: function (e) { this.model.set("name", this.$el.val()); }
});
var PresenterView = Backbone.View.extend({
initialize: function () { this.listenTo(this.model, "change:name", this.render); },
render: function () { this.$el.html(this.model.get("name").length); return this; }
});
var model = new SimpleModel();
var mView = new ManipulatorView({ el: "#input", model: model });
var pView = new PresenterView({ el: "#output", model: model }).render();
(fiddle)