Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Extract BaseEvents from Base.

Move the observablility of `Y.Base`'s lifecycle and attribute changes
into `Y.BaseEvents`, an extension for `Y.BaseCore`.
  • Loading branch information...
commit f1544a1f7cba6456aa50683ec2f214ff70c68060 1 parent 2d9f814
@ericf authored
Showing with 188 additions and 180 deletions.
  1. +150 −0 src/base/js/Base.js
  2. +1 −0  src/base/js/BaseCore.js
  3. +37 −180 src/base/js/BaseEvents.js
View
150 src/base/js/Base.js
@@ -0,0 +1,150 @@
+ /**
+ * The base module provides the Base class, which objects requiring attribute and custom event support can extend.
+ * The module also provides two ways to reuse code - It augments Base with the Plugin.Host interface which provides
+ * plugin support and also provides the BaseCore.build method which provides a way to build custom classes using extensions.
+ *
+ * @module base
+ */
+
+ /**
+ * The base-base submodule provides the Base class without the Plugin support, provided by Plugin.Host,
+ * and without the extension support provided by BaseCore.build.
+ *
+ * @module base
+ * @submodule base-base
+ */
+
+ /**
+ * <p>
+ * A base class which objects requiring attributes and custom event support can
+ * extend. Base also handles the chaining of initializer and destructor methods across
+ * the hierarchy as part of object construction and destruction. Additionally, attributes configured
+ * through the static <a href="#property_ATTRS">ATTRS</a> property for each class
+ * in the hierarchy will be initialized by Base.
+ * </p>
+ *
+ * <p>
+ * The static <a href="#property_NAME">NAME</a> property of each class extending
+ * from Base will be used as the identifier for the class, and is used by Base to prefix
+ * all events fired by instances of that class.
+ * </p>
+ *
+ * @class Base
+ * @constructor
+ * @uses BaseCore
+ * @uses BaseEvents
+ * @uses Attribute
+ * @uses AttributeCore
+ * @uses AttributeEvents
+ * @uses AttributeExtras
+ * @uses EventTarget
+ *
+ * @param {Object} config Object with configuration property name/value pairs. The object can be
+ * used to provide default values for the objects published attributes.
+ *
+ * <p>
+ * The config object can also contain the following non-attribute properties, providing a convenient
+ * way to configure events listeners and plugins for the instance, as part of the constructor call:
+ * </p>
+ *
+ * <dl>
+ * <dt>on</dt>
+ * <dd>An event name to listener function map, to register event listeners for the "on" moment of the event. A constructor convenience property for the <a href="Base.html#method_on">on</a> method.</dd>
+ * <dt>after</dt>
+ * <dd>An event name to listener function map, to register event listeners for the "after" moment of the event. A constructor convenience property for the <a href="Base.html#method_after">after</a> method.</dd>
+ * <dt>bubbleTargets</dt>
+ * <dd>An object, or array of objects, to register as bubble targets for bubbled events fired by this instance. A constructor convenience property for the <a href="EventTarget.html#method_addTarget">addTarget</a> method.</dd>
+ * <dt>plugins</dt>
+ * <dd>A plugin, or array of plugins to be plugged into the instance (see PluginHost's plug method for signature details). A constructor convenience property for the <a href="Plugin.Host.html#method_plug">plug</a> method.</dd>
+ * </dl>
+ */
+ function Base() {
+ Y.BaseCore.apply(this, arguments);
+ Y.BaseEvents.apply(this, arguments);
+ }
+
+ /**
+ * The list of properties which can be configured for
+ * each attribute (e.g. setter, getter, writeOnce, readOnly etc.)
+ *
+ * @property _ATTR_CFG
+ * @type Array
+ * @static
+ * @private
+ */
+ Base._ATTR_CFG = Y.BaseCore._ATTR_CFG.concat(Y.BaseEvents._ATTR_CFG);
+ Base._ATTR_CFG_HASH = Y.Array.hash(Base._ATTR_CFG);
+
+ /**
+ * The array of non-attribute configuration properties supported by this class.
+ *
+ * `Base` supports "on", "after", "plugins" and "bubbleTargets" properties,
+ * which are not set up as attributes.
+ *
+ * This property is primarily required so that when
+ * <a href="#property__allowAdHocAttrs">`_allowAdHocAttrs`</a> is enabled by
+ * a class, non-attribute configurations don't get added as ad-hoc attributes.
+ *
+ * @property _NON_ATTRS_CFG
+ * @type Array
+ * @static
+ * @private
+ */
+ Base._NON_ATTRS_CFG = Y.BaseCore._NON_ATTRS_CFG.concat(Y.BaseEvents._NON_ATTR_CFG);
+
+ /**
+ * <p>
+ * The string to be used to identify instances of
+ * this class, for example in prefixing events.
+ * </p>
+ * <p>
+ * Classes extending Base, should define their own
+ * static NAME property, which should be camelCase by
+ * convention (e.g. MyClass.NAME = "myClass";).
+ * </p>
+ * @property NAME
+ * @type String
+ * @static
+ */
+ Base.NAME = "base";
+
+ /**
+ * The default set of attributes which will be available for instances of this class, and
+ * their configuration. In addition to the configuration properties listed by
+ * Attribute's <a href="Attribute.html#method_addAttr">addAttr</a> method, the attribute
+ * can also be configured with a "cloneDefaultValue" property, which defines how the statically
+ * defined value field should be protected ("shallow", "deep" and false are supported values).
+ *
+ * By default if the value is an object literal or an array it will be "shallow" cloned, to
+ * protect the default value.
+ *
+ * @property ATTRS
+ * @type Object
+ * @static
+ */
+ Base.ATTRS = Y.AttributeCore.prototype._protectAttrs(Y.BaseCore.ATTRS);
+
+ Base.prototype = {
+
+ /**
+ * Utility method to define the attribute hash used to filter/whitelist property mixes for
+ * this class.
+ *
+ * @method _attrCfgHash
+ * @private
+ */
+ _attrCfgHash: function() {
+ return Base._ATTR_CFG_HASH;
+ }
+
+ };
+
+ Y.mix(Base, Y.BaseCore, false, null, 1);
+
+ // Needs to be `true`, to overwrite methods from `BaseCore`.
+ Y.mix(Base, Y.BaseEvents, true, null, 1);
+
+ // Fix constructor
+ Base.prototype.constructor = Base;
+
+ Y.Base = Base;
View
1  src/base/js/BaseCore.js
@@ -180,6 +180,7 @@
* @private
*/
_initBase : function(config) {
+ Y.log('init called', 'life', 'base');
Y.stamp(this);
View
217 src/base/js/BaseEvents.js
@@ -1,34 +1,10 @@
/**
- * The base module provides the Base class, which objects requiring attribute and custom event support can extend.
- * The module also provides two ways to reuse code - It augments Base with the Plugin.Host interface which provides
- * plugin support and also provides the BaseCore.build method which provides a way to build custom classes using extensions.
- *
- * @module base
- */
+ The `base-events` submodule adds observability to Base's lifecycle and
+ attributes, and also make it an `EventTarget`.
- /**
- * The base-base submodule provides the Base class without the Plugin support, provided by Plugin.Host,
- * and without the extension support provided by BaseCore.build.
- *
- * @module base
- * @submodule base-base
- */
-
- /**
- * The base module provides the Base class, which objects requiring attribute and custom event support can extend.
- * The module also provides two ways to reuse code - It augments Base with the Plugin.Host interface which provides
- * plugin support and also provides the Base.build method which provides a way to build custom classes using extensions.
- *
- * @module base
- */
-
- /**
- * The base-base submodule provides the Base class without the Plugin support, provided by Plugin.Host,
- * and without the extension support provided by Base.build.
- *
- * @module base
- * @submodule base-base
- */
+ @module base
+ @submodule base-events
+ **/
var L = Y.Lang,
DESTROY = "destroy",
@@ -37,160 +13,45 @@
BUBBLETARGETS = "bubbleTargets",
_BUBBLETARGETS = "_bubbleTargets",
- BaseCore = Y.BaseCore,
- AttributeCore = Y.AttributeCore,
- Attribute = Y.Attribute;
-
- /**
- * <p>
- * A base class which objects requiring attributes and custom event support can
- * extend. Base also handles the chaining of initializer and destructor methods across
- * the hierarchy as part of object construction and destruction. Additionally, attributes configured
- * through the static <a href="#property_ATTRS">ATTRS</a> property for each class
- * in the hierarchy will be initialized by Base.
- * </p>
- *
- * <p>
- * The static <a href="#property_NAME">NAME</a> property of each class extending
- * from Base will be used as the identifier for the class, and is used by Base to prefix
- * all events fired by instances of that class.
- * </p>
- *
- * @class Base
- * @constructor
- * @uses BaseCore
- * @uses Attribute
- * @uses AttributeCore
- * @uses AttributeEvents
- * @uses AttributeExtras
- * @uses EventTarget
- *
- * @param {Object} config Object with configuration property name/value pairs. The object can be
- * used to provide default values for the objects published attributes.
- *
- * <p>
- * The config object can also contain the following non-attribute properties, providing a convenient
- * way to configure events listeners and plugins for the instance, as part of the constructor call:
- * </p>
- *
- * <dl>
- * <dt>on</dt>
- * <dd>An event name to listener function map, to register event listeners for the "on" moment of the event. A constructor convenience property for the <a href="Base.html#method_on">on</a> method.</dd>
- * <dt>after</dt>
- * <dd>An event name to listener function map, to register event listeners for the "after" moment of the event. A constructor convenience property for the <a href="Base.html#method_after">after</a> method.</dd>
- * <dt>bubbleTargets</dt>
- * <dd>An object, or array of objects, to register as bubble targets for bubbled events fired by this instance. A constructor convenience property for the <a href="EventTarget.html#method_addTarget">addTarget</a> method.</dd>
- * <dt>plugins</dt>
- * <dd>A plugin, or array of plugins to be plugged into the instance (see PluginHost's plug method for signature details). A constructor convenience property for the <a href="Plugin.Host.html#method_plug">plug</a> method.</dd>
- * </dl>
- */
- function Base() {
- BaseCore.apply(this, arguments);
- }
-
- /**
- * The list of properties which can be configured for
- * each attribute (e.g. setter, getter, writeOnce, readOnly etc.)
- *
- * @property _ATTR_CFG
- * @type Array
- * @static
- * @private
- */
- Base._ATTR_CFG = Attribute._ATTR_CFG.concat("cloneDefaultValue");
- Base._ATTR_CFG_HASH = Y.Array.hash(Base._ATTR_CFG);
+ Attribute = Y.Attribute,
+ AttributeEvents = Y.AttributeEvents;
/**
- * The array of non-attribute configuration properties supported by this class.
- *
- * `Base` supports "on", "after", "plugins" and "bubbleTargets" properties,
- * which are not set up as attributes.
- *
- * This property is primarily required so that when
- * <a href="#property__allowAdHocAttrs">`_allowAdHocAttrs`</a> is enabled by
- * a class, non-attribute configurations don't get added as ad-hoc attributes.
- *
- * @property _NON_ATTRS_CFG
- * @type Array
- * @static
- * @private
- */
- Base._NON_ATTRS_CFG = BaseCore._NON_ATTRS_CFG.concat(["on", "after", "bubbleTargets"]);
+ Provides an augmentable implementation of lifecycle and attribute events for
+ `BaseCore`.
- /**
- * <p>
- * The string to be used to identify instances of
- * this class, for example in prefixing events.
- * </p>
- * <p>
- * Classes extending Base, should define their own
- * static NAME property, which should be camelCase by
- * convention (e.g. MyClass.NAME = "myClass";).
- * </p>
- * @property NAME
- * @type String
- * @static
- */
- Base.NAME = "base";
+ @class BaseEvents
+ @extensionfor BaseCore
+ @uses Attribute
+ @uses AttributeCore
+ @uses AttributeEvents
+ @uses AttributeExtras
+ @uses EventTarget
+ @since 3.7.0
+ **/
+ function BaseEvents() {}
- /**
- * The default set of attributes which will be available for instances of this class, and
- * their configuration. In addition to the configuration properties listed by
- * Attribute's <a href="Attribute.html#method_addAttr">addAttr</a> method, the attribute
- * can also be configured with a "cloneDefaultValue" property, which defines how the statically
- * defined value field should be protected ("shallow", "deep" and false are supported values).
- *
- * By default if the value is an object literal or an array it will be "shallow" cloned, to
- * protect the default value.
- *
- * @property ATTRS
- * @type Object
- * @static
- */
- Base.ATTRS = AttributeCore.prototype._protectAttrs(BaseCore.ATTRS);
+ BaseEvents._ATTR_CFG = AttributeEvents._ATTR_CFG.concat();
+ BaseEvents._NON_ATTRS_CFG = ["on", "after", "bubbleTargets"];
- Base.prototype = {
+ BaseEvents.prototype = {
/**
- * Internal construction logic for Base.
+ * Initializes Attribute
*
- * @method _initBase
- * @param {Object} config The constructor configuration object
- * @private
- */
- _initBase: function(cfg) {
- Y.log('init called', 'life', 'base');
-
- this._eventPrefix = this.constructor.EVENT_PREFIX || this.constructor.NAME;
-
- Y.BaseCore.prototype._initBase.call(this, cfg);
- },
-
- /**
- * Initializes Attribute
- *
* @method _initAttribute
* @private
*/
_initAttribute: function(cfg) {
Attribute.call(this);
- this._yuievt.config.prefix = this._eventPrefix;
- },
- /**
- * Utility method to define the attribute hash used to filter/whitelist property mixes for
- * this class.
- *
- * @method _attrCfgHash
- * @private
- */
- _attrCfgHash: function() {
- return Base._ATTR_CFG_HASH;
+ 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
+ * Fires the init event prior to setting up attributes and
* invoking initializers for the class hierarchy.
*
* @method init
@@ -201,8 +62,8 @@
init: function(config) {
/**
* <p>
- * Lifecycle event for the init phase, fired prior to initialization.
- * Invoking the preventDefault() method on the event object provided
+ * 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>
@@ -213,7 +74,7 @@
*
* @event init
* @preventable _defInitFn
- * @param {EventFacade} e Event object, with a cfg property which
+ * @param {EventFacade} e Event object, with a cfg property which
* refers to the configuration object passed to the constructor.
*/
this.publish(INIT, {
@@ -232,7 +93,7 @@
/**
* 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
+ * easily configure on and after listeners as well as bubble targets during
* construction, prior to init.
*
* @private
@@ -255,7 +116,7 @@
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++) {
+ for (i = 0, l = target.length; i < l; i++) {
this.addTarget(target[i]);
}
} else if (target) {
@@ -284,9 +145,9 @@
/**
* <p>
- * Lifecycle event for the destroy phase,
- * fired prior to destruction. Invoking the preventDefault
- * method on the event object provided to subscribers will
+ * 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>
@@ -314,7 +175,7 @@
* Default init event handler
*
* @method _defInitFn
- * @param {EventFacade} e Event object, with a cfg property which
+ * @param {EventFacade} e Event object, with a cfg property which
* refers to the configuration object passed to the constructor.
* @protected
*/
@@ -334,10 +195,6 @@
}
};
- Y.mix(Base, Attribute, false, null, 1);
- Y.mix(Base, BaseCore, false, null, 1);
-
- // Fix constructor
- Base.prototype.constructor = Base;
+ Y.mix(BaseEvents, Attribute, false, null, 1);
- Y.Base = Base;
+ Y.BaseEvents = BaseEvents;
Please sign in to comment.
Something went wrong with that request. Please try again.