I have a top level PageView that will re-render itself whenever the route changes. I have many nested sub-views embedded into this PageView. If I was to re-render PageView, do I need to remove/unbind all the nested sub-views along with the PageView or do I only need to remove/unbind the PageView? If I need to remove/unbind all the sub-views, what is the best way to go about doing this?
标签:
backbone.js
相关问题
- Backbone.js PushState routes .htaccess only workin
- Updating a LayoutView's Model
- Disable Backbone.js hashes entirely, but keep push
- Is there an easy way to distribute a Flask server
- React + Backbone, Target container is not a DOM el
相关文章
- Get all models in backbone collection where attrib
- How can I dynamically set a className for a Backbo
- Nesting Views within Views in backbone js
- Backbone-relational hasmany best practices
- JavaScript error: “is not a constructor”
- Marionette + i18n in templates
- Rendering reCAPTCHA v2.0 widget within Backbone vi
- Backbone.js PUT/DELETE problems with Codeigniter R
A simple and modular class you might find useful.
usage:
Yes, you need to properly remove and unbind them:
http://lostechies.com/derickbailey/2011/09/15/zombies-run-managing-page-transitions-in-backbone-apps/
The easy way to do this is to store an array of your sub-views in the parent view. Then in a
close
method on the parent view, loop through the array and call aclose
method on the child views:Be sure to call the
close
method on the parent view when you are ready for it to be removed / replaced. This will ensure all of the children are cleaned up properly (assuming all of them have their ownclose
method).Instead of keeping an array of children view, could iterate through all the properties of your view and see which ones are an instance of Backbone.View; you will need to make sure to set a property for each child view in your parent view.
In the example below, the child views are set to properties of the parent view. I'm not sure what the performance hit will be looping through all the properties, however, it may be easier than keeping track of a separate data structure for the child views.
Example:
A bit like Zengineer wrote, i like to patch Backbone.View.remove globally like the following, so that any child views attached on this are removed