Backbone Subview-Manager
Minimalist mixin and abstract view implementation to manager subviews in your Backbone application.
Installation
- With
npm
:npm install backbone-subview-manager --save
. - With
bower
:bower install backbone-subview-manager --save
.
Once installed, import the library using ES6 import, or import the ES5 script (transpiled with babel):
View
Here is an example:
;;;; { thiscollection = ; this; thiscollection; } { return 'ul'; } { this; this; }
What happens here?
- Each element in the collection is rendered in its own view.
- Once a view is appened, it is automatically registered using the
addSubView
method. - When the main is removed, each subviews will be automatically destroyed (using their
remove
method) to avoid memory leaks.
And that's all!
Note that you can also call initSubView
to create the view automatically. The following code is exactly the same as the previous one:
;;;; { thiscollection = ; this; thiscollection; } { return 'ul'; } { this; thiscollection; }
API
The following methods can be used on any CompositeView
:
[this] addSubViews(views)
Register subview or array of subview that will be automatically removed when the parent view is removed.
[view] initSubView(ViewImpl, options)
Create the subview (using options
as constructor parameter), and register it using the addSubView
method.
[this] removeSubViews(views)
Remove the subview, or array of subviews (the subview may be the view instance or the view cid). Calling this function without parameters will remove all subviews.
History
This little library has been created in 2013 (still used in production) and open sourced in 2016 after a rewrite in ES6.
License
MIT.