Browse files

add closeView method

  • Loading branch information...
1 parent bcc944f commit e13cfecb228533b540c00f94059e65501cc93a26 Dao Xuan Dung committed Aug 29, 2012
Showing with 8 additions and 1 deletion.
  1. +8 −1
@@ -19,7 +19,9 @@ $(this.el).append(productView.render().el);
This code, although simple, will create zombie view, because we constantly create new view without remove previous view. If you are not familiar with zombie view, you might refer to an excellent explanation here:
-Using View Manager, you can now do like this:
+However it's not convenient to call view.dispose() for all Backbone Views, especially if we create views in for loop.
+I think the best timing to put cleanup code is before creating new view. My solution is to create a View Manager helper to do this cleanup:
ProductView = Backbone.View.extend({…});
productView = VM.createView("productView", function() {
@@ -40,5 +42,10 @@ $(this.el).append(productView.render().el);
The different between VM.reuseView and VM.createView is that, reuseView will look for existing view with name "productView", if found it will return to you. Else, it will execute the callback function and cache result. VM.createView will always execute the callback function and cleanup existing view for you. Hence you may like to use VM.createView if the views is dynamic and frequently change
+Sometime we just need to close existing view, there is a simple function for it:
# About
I got inspired by implementation from You might like to check it out :)

0 comments on commit e13cfec

Please sign in to comment.