-
Notifications
You must be signed in to change notification settings - Fork 0
/
bubble-view.js
50 lines (39 loc) · 1.21 KB
/
bubble-view.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
39
40
41
42
43
44
45
46
47
48
49
50
var BubbleView;
if (!Backbone){
throw new Error('Backbone is not defined!');
}
BubbleView = Backbone.View.extend({
bindToParent: function(parent){
var self = this,
i, event;
this.boundMethods || (this.boundMethods = {});
this.parent = parent;
_.each(this.constructor.bubbleEvents, function(event){
self._makeBoundMethod(event);
});
},
_makeBoundMethod: function(event){
var boundMetod = function(){
if (typeof this[event] == "function"){
this[event]();
}
/* bubble on, for the kids, even if not implemented on this instance */
this.trigger(event);
};
this.boundMethods[event] = boundMetod;
this.parent.bind(event, boundMetod, this);
},
unbindFromParent: function(){
var event;
if (!this.boundMethods) return;
for (event in this.boundMethods){
if (this.boundMethods.hasOwnProperty(event)){
this.parent.unbind(event, this.boundMethods[event], this);
delete this.boundMethods[event];
}
}
delete this.parent;
}
}, {
bubbleEvents: []
});