MyView = Backbone.View.extend({ | |
events: { | |
"click #someButton": "doThat", | |
"change #someInput": "changeIt" | |
}, | |
doThat: function(){ ... }, | |
changeIt: function(){ ... } | |
}); |
MyView = Backbone.View.extend({ | |
initialize: function(){ | |
this.model.bind("change", this.render, this); | |
}, | |
render: function(){ ... } | |
}); |
MyView = Backbone.View.extend({ | |
initialize: function(){ | |
this.options.vent.bind("app:event", this.render, this); | |
}, | |
render: function() { ... } | |
}); | |
var vent = new _.extend({}, Backbone.Events); | |
new MyView({vent: vent}); | |
vent.trigger("app:event"); |
MyRouter = Backbone.Router.extend({ | |
routes: { | |
"": "home", | |
"post/:id": "showPost" | |
}, | |
home: function(){ | |
var homeView = new HomeView(); | |
$("#mainContent").html(homeView.render().el); | |
}, | |
showPost: function(id){ | |
var post = posts.get(id); | |
var postView = new PostView({model: post}); | |
$("#mainContent").html(postView.render().el); | |
} | |
}); |
function AppView(){ | |
this.showView(view) { | |
if (this.currentView){ | |
this.currentView.close(); | |
} | |
this.currentView = view; | |
this.currentView.render(); | |
$("#mainContent").html(this.currentView.el); | |
} | |
} |
MyRouter = Backbone.Router.extend({ | |
routes: { | |
"": "home", | |
"post/:id": "showPost" | |
}, | |
initialize: function(options){ | |
this.appView = options.appView; | |
}, | |
home: function(){ | |
var homeView = new HomeView(); | |
this.appView.showView(homeView); | |
}, | |
showPost: function(id){ | |
var post = posts.get(id); | |
var postView = new PostView({model: post}); | |
this.appView.showView(postView); | |
} | |
}); |
Backbone.View.prototype.close = function(){ | |
this.remove(); | |
this.unbind(); | |
} |
Backbone.View.prototype.close = function(){ | |
this.remove(); | |
this.unbind(); | |
if (this.onClose){ | |
this.onClose(); | |
} | |
} |
MyView = Backbone.View.extend({ | |
initialize: function(){ | |
this.model.bind("change", this.render, this); | |
}, | |
render: function(){ ... }, | |
onClose: function(){ | |
this.model.unbind("change", this.render); | |
} | |
}); |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment Hide comment
eskimoblood
commented
Nov 30, 2011
At your last example, wouldn' it be better to have an event object for models and/or collections like the one for the DOM events. Then you have a start and stop method where you can simple bind/unbind this events automatically. Its inspired by the architecture of GWT where every activity has a start and an optional stop method. https://gist.github.com/1410703 |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment Hide comment
derickbailey
Nov 30, 2011
at the time I wrote this, that idea hadn't crossed my mind... but I'm doing basically that, now. it works really well. I'm using a combination of both techniques, depending on the situation.
at the time I wrote this, that idea hadn't crossed my mind... but I'm doing basically that, now. it works really well. I'm using a combination of both techniques, depending on the situation. |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment Hide comment
eskimoblood
Nov 30, 2011
Btw, is it really necessary to delete the DOM events. Aren't they gone when the element is deleted?
eskimoblood
commented
Nov 30, 2011
Btw, is it really necessary to delete the DOM events. Aren't they gone when the element is deleted? |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment Hide comment
derickbailey
Nov 30, 2011
i don't know if remove the elements from the DOM would clean them up directly or not. i know calling this.remove()
will force jquery to clean them up and i can only imagine that jquery implemented the DOM event cleanup for a reason... but i don't know for sure
i don't know if remove the elements from the DOM would clean them up directly or not. i know calling |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment Hide comment
philfreo
commented
Jun 27, 2012
You're missing the word |
At your last example, wouldn' it be better to have an event object for models and/or collections like the one for the DOM events. Then you have a start and stop method where you can simple bind/unbind this events automatically. Its inspired by the architecture of GWT where every activity has a start and an optional stop method. https://gist.github.com/1410703