Created
September 15, 2011 15:37
-
-
Save mxriverlynn/1219582 to your computer and use it in GitHub Desktop.
zombies! run!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MyView = Backbone.View.extend({ | |
events: { | |
"click #someButton": "doThat", | |
"change #someInput": "changeIt" | |
}, | |
doThat: function(){ ... }, | |
changeIt: function(){ ... } | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MyView = Backbone.View.extend({ | |
initialize: function(){ | |
this.model.bind("change", this.render, this); | |
}, | |
render: function(){ ... } | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function AppView(){ | |
this.showView(view) { | |
if (this.currentView){ | |
this.currentView.close(); | |
} | |
this.currentView = view; | |
this.currentView.render(); | |
$("#mainContent").html(this.currentView.el); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Backbone.View.prototype.close = function(){ | |
this.remove(); | |
this.unbind(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Backbone.View.prototype.close = function(){ | |
this.remove(); | |
this.unbind(); | |
if (this.onClose){ | |
this.onClose(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MyView = Backbone.View.extend({ | |
initialize: function(){ | |
this.model.bind("change", this.render, this); | |
}, | |
render: function(){ ... }, | |
onClose: function(){ | |
this.model.unbind("change", this.render); | |
} | |
}); |
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
You're missing the word function
in 5-appview.js
(this.showView(view) {
)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Btw, is it really necessary to delete the DOM events. Aren't they gone when the element is deleted?