Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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...
commit d92409c2b72b11f582a066b7b7b94049ad06e9aa 1 parent b1e13ff
Eric Ferraiuolo authored June 25, 2012
16  src/base/js/Base.js
@@ -73,7 +73,6 @@
73 73
      * @private
74 74
      */
75 75
     Base._ATTR_CFG      = Y.BaseCore._ATTR_CFG.concat(Y.BaseEvents._ATTR_CFG);
76  
-    Base._ATTR_CFG_HASH = Y.Array.hash(Base._ATTR_CFG);
77 76
 
78 77
     /**
79 78
      * The array of non-attribute configuration properties supported by this class.
@@ -124,21 +123,6 @@
124 123
      */
125 124
     Base.ATTRS = Y.AttributeCore.prototype._protectAttrs(Y.BaseCore.ATTRS);
126 125
 
127  
-    Base.prototype = {
128  
-
129  
-        /**
130  
-         * Utility method to define the attribute hash used to filter/whitelist property mixes for
131  
-         * this class.
132  
-         *
133  
-         * @method _attrCfgHash
134  
-         * @private
135  
-         */
136  
-        _attrCfgHash: function() {
137  
-            return Base._ATTR_CFG_HASH;
138  
-        }
139  
-
140  
-    };
141  
-
142 126
     Y.mix(Base, Y.BaseCore, false, null, 1);
143 127
 
144 128
     // Needs to be `true`, to overwrite methods from `BaseCore`.
10  src/base/js/BaseBuild.js
@@ -7,7 +7,8 @@
7 7
      * @submodule base-build
8 8
      * @for Base
9 9
      */
10  
-    var Base = Y.Base,
  10
+    var BaseCore = Y.BaseCore,
  11
+        Base = Y.Base,
11 12
         L = Y.Lang,
12 13
         INITIALIZER = "initializer",
13 14
         DESTRUCTOR = "destructor",
@@ -15,7 +16,7 @@
15 16
         arrayAggregator = function (prop, r, s) {
16 17
             if (s[prop]) {
17 18
                 r[prop] = (r[prop] || []).concat(s[prop]);
18  
-            }    
  19
+            }
19 20
         };
20 21
 
21 22
     Base._build = function(name, main, extensions, px, sx, cfg) {
@@ -391,10 +392,9 @@
391 392
      * @final
392 393
      * @private
393 394
      */
394  
-    Base._buildCfg = {
  395
+    Base._buildCfg = BaseCore._buildCfg = {
395 396
         custom : {
396 397
             ATTRS : function(prop, r, s) {
397  
-
398 398
                 r.ATTRS = r.ATTRS || {};
399 399
 
400 400
                 if (s.ATTRS) {
@@ -411,6 +411,8 @@
411 411
                     }
412 412
                 }
413 413
             },
  414
+
  415
+            _ATTR_CFG : arrayAggregator,
414 416
             _NON_ATTRS_CFG : arrayAggregator
415 417
         },
416 418
         aggregates : ["_PLUG", "_UNPLUG"]
37  src/base/js/BaseCore.js
@@ -81,7 +81,6 @@
81 81
      * @private
82 82
      */
83 83
     BaseCore._ATTR_CFG = AttributeCore._ATTR_CFG.concat("cloneDefaultValue");
84  
-    BaseCore._ATTR_CFG_HASH = Y.Array.hash(BaseCore._ATTR_CFG);
85 84
 
86 85
     /**
87 86
      * The array of non-attribute configuration properties supported by this class. 
@@ -309,6 +308,18 @@
309 308
             return this._attrs;
310 309
         },
311 310
 
  311
+
  312
+        /**
  313
+         * Utility method to define the attribute hash used to filter/whitelist property mixes for 
  314
+         * this class. 
  315
+         * 
  316
+         * @method _getAttrCfgHash
  317
+         * @private
  318
+         */
  319
+        _getAttrCfgHash: function() {
  320
+            return this._attrCfgHash;
  321
+        },
  322
+
312 323
         /**
313 324
          * A helper method used when processing ATTRS across the class hierarchy during 
314 325
          * initialization. Returns a disposable object with the attributes defined for 
@@ -383,6 +394,8 @@
383 394
             var c = this.constructor,
384 395
                 i,
385 396
                 l,
  397
+                attrCfg,
  398
+                attrCfgHash = {},
386 399
                 nonAttrsCfg,
387 400
                 nonAttrs = (this._allowAdHocAttrs) ? {} : null,
388 401
                 classes = [],
@@ -397,6 +410,14 @@
397 410
                     attrs[attrs.length] = c.ATTRS;
398 411
                 }
399 412
 
  413
+                // Aggregate ATTR cfg whitelist.
  414
+                attrCfg = c._ATTR_CFG;
  415
+                if (attrCfg) {
  416
+                    for (i = 0, l = attrCfg.length; i < l; i += 1) {
  417
+                        attrCfgHash[attrCfg[i]] = true;
  418
+                    }
  419
+                }
  420
+
400 421
                 if (this._allowAdHocAttrs) {
401 422
                     nonAttrsCfg = c._NON_ATTRS_CFG; 
402 423
                     if (nonAttrsCfg) {
@@ -410,22 +431,12 @@
410 431
             }
411 432
 
412 433
             this._classes = classes;
  434
+            this._attrCfgHash = attrCfgHash;
413 435
             this._nonAttrs = nonAttrs;
414 436
             this._attrs = this._aggregateAttrs(attrs);
415 437
         },
416 438
 
417 439
         /**
418  
-         * Utility method to define the attribute hash used to filter/whitelist property mixes for 
419  
-         * this class. 
420  
-         * 
421  
-         * @method _attrCfgHash
422  
-         * @private
423  
-         */
424  
-        _attrCfgHash: function() {
425  
-            return BaseCore._ATTR_CFG_HASH;
426  
-        },
427  
-
428  
-        /**
429 440
          * A helper method, used by _initHierarchyData to aggregate 
430 441
          * attribute configuration across the instances class hierarchy.
431 442
          *
@@ -447,7 +458,7 @@
447 458
                 path,
448 459
                 i,
449 460
                 clone,
450  
-                cfgPropsHash = this._attrCfgHash(),
  461
+                cfgPropsHash = this._getAttrCfgHash(),
451 462
                 aggAttr,
452 463
                 aggAttrs = {};
453 464
 
53  src/base/tests/base-tests.js
@@ -1456,6 +1456,59 @@ YUI.add('base-tests', function(Y) {
1456 1456
 
1457 1457
             /* Currently broken. Will fix in 3.6.0pr1. Too late at this point, given the extreme edge caseness. */
1458 1458
             // Y.Assert.areEqual("abc", MyBuiltClass.MY_CUSTOM);
  1459
+        },
  1460
+
  1461
+        "test:base-core-subclass": function () {
  1462
+            var calls = 0,
  1463
+                Foo, foo;
  1464
+
  1465
+            Foo = Y.Base.create('foo', Y.BaseCore, [], {
  1466
+                initializer: function () {
  1467
+                    calls += 1;
  1468
+                }
  1469
+            }, {
  1470
+                ATTRS: {
  1471
+                    bar: {
  1472
+                        value: 'bar',
  1473
+                        getter: function (val) {
  1474
+                            return val.toUpperCase();
  1475
+                        }
  1476
+                    }
  1477
+                }
  1478
+            });
  1479
+
  1480
+            foo = new Foo();
  1481
+
  1482
+            Y.Assert.areSame('BAR', foo.get('bar'));
  1483
+            Y.Assert.areSame(1, calls);
  1484
+        },
  1485
+
  1486
+        "test:base-core-with-base-events-ext": function () {
  1487
+            var calls = 0,
  1488
+                Foo, foo;
  1489
+
  1490
+            Foo = Y.Base.create('foo', Y.BaseCore, [Y.BaseEvents], {
  1491
+                initializer: function () {
  1492
+                    this.after('barChange', function () {
  1493
+                        calls += 1;
  1494
+                    });
  1495
+                }
  1496
+            }, {
  1497
+                ATTRS: {
  1498
+                    bar: {
  1499
+                        value: 'bar',
  1500
+                        getter: function (val) {
  1501
+                            return val.toUpperCase();
  1502
+                        }
  1503
+                    }
  1504
+                }
  1505
+            });
  1506
+
  1507
+            foo = new Foo();
  1508
+            foo.set('bar', 'baz');
  1509
+
  1510
+            Y.Assert.areSame('BAZ', foo.get('bar'));
  1511
+            Y.Assert.areSame(1, calls);
1459 1512
         }
1460 1513
 
1461 1514
     }));

0 notes on commit d92409c

Please sign in to comment.
Something went wrong with that request. Please try again.