public
Created

Backbone responsibilities

  • Download Gist
events.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14
Backbone.View.extend({
 
initialize: function(){
this.model.bind("change:foo", this.render, this);
},
 
render: function(){
// render some HTML here
// tell any listeners it happened
this.trigger("it:happened");
}
 
});
history.js
JavaScript
1
Backbone.history.start();
memorystate.js
JavaScript
1 2 3 4 5
myModel.set({someAttr: "some value"});
 
// ... later in the code ...
 
alert(myModel.get("someAttr")); // => alerts "some value"
navigate.js
JavaScript
1
myRouter.navigate("some/route");
persistence.js
JavaScript
1 2 3 4 5
// save changes to the model, back to a server
myModel.save();
 
// get all of the models for this collection, from a server
myCollection.fetch();
router.js
JavaScript
1 2 3 4 5 6 7 8 9
MyRouter = Backbone.Router.extend({
routes: {
"some/route": "someMethod"
},
 
someMethod: function(){
// do stuff here
}
});
vent.js
JavaScript
1 2 3 4 5 6 7 8 9
var vent = _.extend({}, Backbone.Events);
 
vent.bind("foo", function(){
alert("bar");
});
 
 
// ... later in the code
vent.trigger("foo"); // => alerts "bar"
view.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13
MyView = Backbone.View.extend({
events: {
"click #someElement": "someMethod"
},
 
someMethod: function(){
alert("you clicked it!");
},
 
render: function(){
$(this.el).html("<button id='someElement' value='click me'></button>");
}
});

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.