API Docs for: 3.13.0
Show:

File: base/js/BaseObservable.js

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