|
1 /* |
|
2 YUI 3.10.3 (build 2fb5187) |
|
3 Copyright 2013 Yahoo! Inc. All rights reserved. |
|
4 Licensed under the BSD License. |
|
5 http://yuilibrary.com/license/ |
|
6 */ |
|
7 |
|
8 YUI.add('base-observable', function (Y, NAME) { |
|
9 |
|
10 /** |
|
11 The `base-observable` submodule adds observability to Base's lifecycle and |
|
12 attributes, and also make it an `EventTarget`. |
|
13 |
|
14 @module base |
|
15 @submodule base-observable |
|
16 **/ |
|
17 var L = Y.Lang, |
|
18 |
|
19 DESTROY = "destroy", |
|
20 INIT = "init", |
|
21 |
|
22 BUBBLETARGETS = "bubbleTargets", |
|
23 _BUBBLETARGETS = "_bubbleTargets", |
|
24 |
|
25 AttributeObservable = Y.AttributeObservable, |
|
26 BaseCore = Y.BaseCore; |
|
27 |
|
28 /** |
|
29 Provides an augmentable implementation of lifecycle and attribute events for |
|
30 `BaseCore`. |
|
31 |
|
32 @class BaseObservable |
|
33 @extensionfor BaseCore |
|
34 @uses AttributeObservable |
|
35 @uses EventTarget |
|
36 @since 3.8.0 |
|
37 **/ |
|
38 function BaseObservable() {} |
|
39 |
|
40 BaseObservable._ATTR_CFG = AttributeObservable._ATTR_CFG.concat(); |
|
41 BaseObservable._NON_ATTRS_CFG = ["on", "after", "bubbleTargets"]; |
|
42 |
|
43 BaseObservable.prototype = { |
|
44 |
|
45 /** |
|
46 * Initializes Attribute |
|
47 * |
|
48 * @method _initAttribute |
|
49 * @private |
|
50 */ |
|
51 _initAttribute: function() { |
|
52 BaseCore.prototype._initAttribute.apply(this, arguments); |
|
53 AttributeObservable.call(this); |
|
54 |
|
55 this._eventPrefix = this.constructor.EVENT_PREFIX || this.constructor.NAME; |
|
56 this._yuievt.config.prefix = this._eventPrefix; |
|
57 }, |
|
58 |
|
59 /** |
|
60 * Init lifecycle method, invoked during construction. |
|
61 * Fires the init event prior to setting up attributes and |
|
62 * invoking initializers for the class hierarchy. |
|
63 * |
|
64 * @method init |
|
65 * @chainable |
|
66 * @param {Object} config Object with configuration property name/value pairs |
|
67 * @return {Base} A reference to this object |
|
68 */ |
|
69 init: function(config) { |
|
70 |
|
71 /** |
|
72 * <p> |
|
73 * Lifecycle event for the init phase, fired prior to initialization. |
|
74 * Invoking the preventDefault() method on the event object provided |
|
75 * to subscribers will prevent initialization from occuring. |
|
76 * </p> |
|
77 * <p> |
|
78 * Subscribers to the "after" momemt of this event, will be notified |
|
79 * after initialization of the object is complete (and therefore |
|
80 * cannot prevent initialization). |
|
81 * </p> |
|
82 * |
|
83 * @event init |
|
84 * @preventable _defInitFn |
|
85 * @param {EventFacade} e Event object, with a cfg property which |
|
86 * refers to the configuration object passed to the constructor. |
|
87 */ |
|
88 |
|
89 // PERF: Using lower level _publish() for |
|
90 // critical path performance |
|
91 |
|
92 var type = this._getFullType(INIT), |
|
93 e = this._publish(type); |
|
94 |
|
95 e.emitFacade = true; |
|
96 e.fireOnce = true; |
|
97 e.defaultTargetOnly = true; |
|
98 e.defaultFn = this._defInitFn; |
|
99 |
|
100 this._preInitEventCfg(config); |
|
101 |
|
102 this.fire(type, {cfg: config}); |
|
103 |
|
104 return this; |
|
105 }, |
|
106 |
|
107 /** |
|
108 * Handles the special on, after and target properties which allow the user to |
|
109 * easily configure on and after listeners as well as bubble targets during |
|
110 * construction, prior to init. |
|
111 * |
|
112 * @private |
|
113 * @method _preInitEventCfg |
|
114 * @param {Object} config The user configuration object |
|
115 */ |
|
116 _preInitEventCfg : function(config) { |
|
117 if (config) { |
|
118 if (config.on) { |
|
119 this.on(config.on); |
|
120 } |
|
121 if (config.after) { |
|
122 this.after(config.after); |
|
123 } |
|
124 } |
|
125 |
|
126 var i, l, target, |
|
127 userTargets = (config && BUBBLETARGETS in config); |
|
128 |
|
129 if (userTargets || _BUBBLETARGETS in this) { |
|
130 target = userTargets ? (config && config.bubbleTargets) : this._bubbleTargets; |
|
131 |
|
132 if (L.isArray(target)) { |
|
133 for (i = 0, l = target.length; i < l; i++) { |
|
134 this.addTarget(target[i]); |
|
135 } |
|
136 } else if (target) { |
|
137 this.addTarget(target); |
|
138 } |
|
139 } |
|
140 }, |
|
141 |
|
142 /** |
|
143 * <p> |
|
144 * Destroy lifecycle method. Fires the destroy |
|
145 * event, prior to invoking destructors for the |
|
146 * class hierarchy. |
|
147 * </p> |
|
148 * <p> |
|
149 * Subscribers to the destroy |
|
150 * event can invoke preventDefault on the event object, to prevent destruction |
|
151 * from proceeding. |
|
152 * </p> |
|
153 * @method destroy |
|
154 * @return {Base} A reference to this object |
|
155 * @chainable |
|
156 */ |
|
157 destroy: function() { |
|
158 Y.log('destroy called', 'life', 'base'); |
|
159 |
|
160 /** |
|
161 * <p> |
|
162 * Lifecycle event for the destroy phase, |
|
163 * fired prior to destruction. Invoking the preventDefault |
|
164 * method on the event object provided to subscribers will |
|
165 * prevent destruction from proceeding. |
|
166 * </p> |
|
167 * <p> |
|
168 * Subscribers to the "after" moment of this event, will be notified |
|
169 * after destruction is complete (and as a result cannot prevent |
|
170 * destruction). |
|
171 * </p> |
|
172 * @event destroy |
|
173 * @preventable _defDestroyFn |
|
174 * @param {EventFacade} e Event object |
|
175 */ |
|
176 this.publish(DESTROY, { |
|
177 fireOnce:true, |
|
178 defaultTargetOnly:true, |
|
179 defaultFn: this._defDestroyFn |
|
180 }); |
|
181 this.fire(DESTROY); |
|
182 |
|
183 this.detachAll(); |
|
184 return this; |
|
185 }, |
|
186 |
|
187 /** |
|
188 * Default init event handler |
|
189 * |
|
190 * @method _defInitFn |
|
191 * @param {EventFacade} e Event object, with a cfg property which |
|
192 * refers to the configuration object passed to the constructor. |
|
193 * @protected |
|
194 */ |
|
195 _defInitFn : function(e) { |
|
196 this._baseInit(e.cfg); |
|
197 }, |
|
198 |
|
199 /** |
|
200 * Default destroy event handler |
|
201 * |
|
202 * @method _defDestroyFn |
|
203 * @param {EventFacade} e Event object |
|
204 * @protected |
|
205 */ |
|
206 _defDestroyFn : function(e) { |
|
207 this._baseDestroy(e.cfg); |
|
208 } |
|
209 }; |
|
210 |
|
211 Y.mix(BaseObservable, AttributeObservable, false, null, 1); |
|
212 |
|
213 Y.BaseObservable = BaseObservable; |
|
214 |
|
215 |
|
216 }, '3.10.3', {"requires": ["attribute-observable"]}); |