forked from tgriesser/backbone-nested
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackbone-nested.js
38 lines (33 loc) · 1.13 KB
/
backbone-nested.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
(function(Backbone, View) {
Backbone.View = View.extend({
// Adds a subview to the current view, which will
// ensure its removal when this view is removed,
// or when view.removeSubviews is called
addSubview: function(view, key) {
if (!(view instanceof View)) {
throw new Error("Subview must be a Backbone.View");
}
if (_.isUndefined(key)) key = view.cid;
(this.subviews || (this.subviews = {}))[key] = (view);
return view;
},
// Removes any subviews associated with this view
// by `addSubview`, which will in-turn remove any
// children of those views, and so on.
removeSubviews: function() {
var children = this.subviews;
if (!children) return this;
_.each(children, function(child) {
child.remove();
});
delete this.subviews;
return this;
},
// Extends the view's remove, by calling `removeSubviews`
// if any subviews exist.
remove: function() {
if (this.subviews) this.removeSubviews();
return View.prototype.remove.apply(this, arguments);
}
});
})(this.Backbone, this.Backbone.View);