/
BaseObservable.js
197 lines (173 loc) · 6.16 KB
/
BaseObservable.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/**
The `base-observable` submodule adds observability to Base's lifecycle and
attributes, and also make it an `EventTarget`.
@module base
@submodule base-observable
**/
var L = Y.Lang,
DESTROY = "destroy",
INIT = "init",
BUBBLETARGETS = "bubbleTargets",
_BUBBLETARGETS = "_bubbleTargets",
AttributeObservable = Y.AttributeObservable;
/**
Provides an augmentable implementation of lifecycle and attribute events for
`BaseCore`.
@class BaseObservable
@extensionfor BaseCore
@uses AttributeObservable
@uses EventTarget
@since 3.7.0
**/
function BaseObservable() {}
BaseObservable._ATTR_CFG = AttributeObservable._ATTR_CFG.concat();
BaseObservable._NON_ATTRS_CFG = ["on", "after", "bubbleTargets"];
BaseObservable.prototype = {
/**
* Initializes Attribute
*
* @method _initAttribute
* @private
*/
_initAttribute: function(cfg) {
Y.BaseCore.prototype._initAttribute.apply(this, arguments);
Y.AttributeObservable.call(this);
this._eventPrefix = this.constructor.EVENT_PREFIX || this.constructor.NAME;
this._yuievt.config.prefix = this._eventPrefix;
},
/**
* Init lifecycle method, invoked during construction.
* Fires the init event prior to setting up attributes and
* invoking initializers for the class hierarchy.
*
* @method init
* @chainable
* @param {Object} config Object with configuration property name/value pairs
* @return {Base} A reference to this object
*/
init: function(config) {
/**
* <p>
* Lifecycle event for the init phase, fired prior to initialization.
* Invoking the preventDefault() method on the event object provided
* to subscribers will prevent initialization from occuring.
* </p>
* <p>
* Subscribers to the "after" momemt of this event, will be notified
* after initialization of the object is complete (and therefore
* cannot prevent initialization).
* </p>
*
* @event init
* @preventable _defInitFn
* @param {EventFacade} e Event object, with a cfg property which
* refers to the configuration object passed to the constructor.
*/
this.publish(INIT, {
queuable:false,
fireOnce:true,
defaultTargetOnly:true,
defaultFn:this._defInitFn
});
this._preInitEventCfg(config);
this.fire(INIT, {cfg: config});
return this;
},
/**
* Handles the special on, after and target properties which allow the user to
* easily configure on and after listeners as well as bubble targets during
* construction, prior to init.
*
* @private
* @method _preInitEventCfg
* @param {Object} config The user configuration object
*/
_preInitEventCfg : function(config) {
if (config) {
if (config.on) {
this.on(config.on);
}
if (config.after) {
this.after(config.after);
}
}
var i, l, target,
userTargets = (config && BUBBLETARGETS in config);
if (userTargets || _BUBBLETARGETS in this) {
target = userTargets ? (config && config.bubbleTargets) : this._bubbleTargets;
if (L.isArray(target)) {
for (i = 0, l = target.length; i < l; i++) {
this.addTarget(target[i]);
}
} else if (target) {
this.addTarget(target);
}
}
},
/**
* <p>
* Destroy lifecycle method. Fires the destroy
* event, prior to invoking destructors for the
* class hierarchy.
* </p>
* <p>
* Subscribers to the destroy
* event can invoke preventDefault on the event object, to prevent destruction
* from proceeding.
* </p>
* @method destroy
* @return {Base} A reference to this object
* @chainable
*/
destroy: function() {
Y.log('destroy called', 'life', 'base');
/**
* <p>
* Lifecycle event for the destroy phase,
* fired prior to destruction. Invoking the preventDefault
* method on the event object provided to subscribers will
* prevent destruction from proceeding.
* </p>
* <p>
* Subscribers to the "after" moment of this event, will be notified
* after destruction is complete (and as a result cannot prevent
* destruction).
* </p>
* @event destroy
* @preventable _defDestroyFn
* @param {EventFacade} e Event object
*/
this.publish(DESTROY, {
queuable:false,
fireOnce:true,
defaultTargetOnly:true,
defaultFn: this._defDestroyFn
});
this.fire(DESTROY);
this.detachAll();
return this;
},
/**
* Default init event handler
*
* @method _defInitFn
* @param {EventFacade} e Event object, with a cfg property which
* refers to the configuration object passed to the constructor.
* @protected
*/
_defInitFn : function(e) {
this._baseInit(e.cfg);
},
/**
* Default destroy event handler
*
* @method _defDestroyFn
* @param {EventFacade} e Event object
* @protected
*/
_defDestroyFn : function(e) {
this._baseDestroy(e.cfg);
}
};
Y.mix(BaseObservable, AttributeObservable, false, null, 1);
Y.BaseObservable = BaseObservable;