Permalink
Browse files

Update `Y.Base.create/mix` to work with `Y.BaseCore`.

This allows BaseCore to be the root Base-based superclass enabling the
ability to mix in BaseEvents at runtime. The following is now possible:

    Y.Foo = Y.Base.create('foo', Y.BaseCore, []);

    // And...

    Y.Bar = Y.Base.create('bar', Y.BaseCore, [Y.BaseEvents]);

    // Or...

    Y.Baz = Y.Base.create('baz', Y.BaseCore, []);
    Y.Base.mix(Y.Baz, [Y.BaseEvents]);
  • Loading branch information...
1 parent b1e13ff commit d92409c2b72b11f582a066b7b7b94049ad06e9aa @ericf committed Jun 25, 2012
Showing with 83 additions and 33 deletions.
  1. +0 −16 src/base/js/Base.js
  2. +6 −4 src/base/js/BaseBuild.js
  3. +24 −13 src/base/js/BaseCore.js
  4. +53 −0 src/base/tests/base-tests.js
View
@@ -73,7 +73,6 @@
* @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.
@@ -124,21 +123,6 @@
*/
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`.
View
@@ -7,15 +7,16 @@
* @submodule base-build
* @for Base
*/
- var Base = Y.Base,
+ var BaseCore = Y.BaseCore,
+ Base = Y.Base,
L = Y.Lang,
INITIALIZER = "initializer",
DESTRUCTOR = "destructor",
build,
arrayAggregator = function (prop, r, s) {
if (s[prop]) {
r[prop] = (r[prop] || []).concat(s[prop]);
- }
+ }
};
Base._build = function(name, main, extensions, px, sx, cfg) {
@@ -391,10 +392,9 @@
* @final
* @private
*/
- Base._buildCfg = {
+ Base._buildCfg = BaseCore._buildCfg = {
custom : {
ATTRS : function(prop, r, s) {
-
r.ATTRS = r.ATTRS || {};
if (s.ATTRS) {
@@ -411,6 +411,8 @@
}
}
},
+
+ _ATTR_CFG : arrayAggregator,
_NON_ATTRS_CFG : arrayAggregator
},
aggregates : ["_PLUG", "_UNPLUG"]
View
@@ -81,7 +81,6 @@
* @private
*/
BaseCore._ATTR_CFG = AttributeCore._ATTR_CFG.concat("cloneDefaultValue");
- BaseCore._ATTR_CFG_HASH = Y.Array.hash(BaseCore._ATTR_CFG);
/**
* The array of non-attribute configuration properties supported by this class.
@@ -309,6 +308,18 @@
return this._attrs;
},
+
+ /**
+ * Utility method to define the attribute hash used to filter/whitelist property mixes for
+ * this class.
+ *
+ * @method _getAttrCfgHash
+ * @private
+ */
+ _getAttrCfgHash: function() {
+ return this._attrCfgHash;
+ },
+
/**
* A helper method used when processing ATTRS across the class hierarchy during
* initialization. Returns a disposable object with the attributes defined for
@@ -383,6 +394,8 @@
var c = this.constructor,
i,
l,
+ attrCfg,
+ attrCfgHash = {},
nonAttrsCfg,
nonAttrs = (this._allowAdHocAttrs) ? {} : null,
classes = [],
@@ -397,6 +410,14 @@
attrs[attrs.length] = c.ATTRS;
}
+ // Aggregate ATTR cfg whitelist.
+ attrCfg = c._ATTR_CFG;
+ if (attrCfg) {
+ for (i = 0, l = attrCfg.length; i < l; i += 1) {
+ attrCfgHash[attrCfg[i]] = true;
+ }
+ }
+
if (this._allowAdHocAttrs) {
nonAttrsCfg = c._NON_ATTRS_CFG;
if (nonAttrsCfg) {
@@ -410,21 +431,11 @@
}
this._classes = classes;
+ this._attrCfgHash = attrCfgHash;
this._nonAttrs = nonAttrs;
this._attrs = this._aggregateAttrs(attrs);
},
- /**
- * Utility method to define the attribute hash used to filter/whitelist property mixes for
- * this class.
- *
- * @method _attrCfgHash
- * @private
- */
- _attrCfgHash: function() {
- return BaseCore._ATTR_CFG_HASH;
- },
-
/**
* A helper method, used by _initHierarchyData to aggregate
* attribute configuration across the instances class hierarchy.
@@ -447,7 +458,7 @@
path,
i,
clone,
- cfgPropsHash = this._attrCfgHash(),
+ cfgPropsHash = this._getAttrCfgHash(),
aggAttr,
aggAttrs = {};
@@ -1456,6 +1456,59 @@ YUI.add('base-tests', function(Y) {
/* Currently broken. Will fix in 3.6.0pr1. Too late at this point, given the extreme edge caseness. */
// Y.Assert.areEqual("abc", MyBuiltClass.MY_CUSTOM);
+ },
+
+ "test:base-core-subclass": function () {
+ var calls = 0,
+ Foo, foo;
+
+ Foo = Y.Base.create('foo', Y.BaseCore, [], {
+ initializer: function () {
+ calls += 1;
+ }
+ }, {
+ ATTRS: {
+ bar: {
+ value: 'bar',
+ getter: function (val) {
+ return val.toUpperCase();
+ }
+ }
+ }
+ });
+
+ foo = new Foo();
+
+ Y.Assert.areSame('BAR', foo.get('bar'));
+ Y.Assert.areSame(1, calls);
+ },
+
+ "test:base-core-with-base-events-ext": function () {
+ var calls = 0,
+ Foo, foo;
+
+ Foo = Y.Base.create('foo', Y.BaseCore, [Y.BaseEvents], {
+ initializer: function () {
+ this.after('barChange', function () {
+ calls += 1;
+ });
+ }
+ }, {
+ ATTRS: {
+ bar: {
+ value: 'bar',
+ getter: function (val) {
+ return val.toUpperCase();
+ }
+ }
+ }
+ });
+
+ foo = new Foo();
+ foo.set('bar', 'baz');
+
+ Y.Assert.areSame('BAZ', foo.get('bar'));
+ Y.Assert.areSame(1, calls);
}
}));

0 comments on commit d92409c

Please sign in to comment.