BaseBuild.js revision c938b255ef3c02ee132e52fbd15bb211c6f3f760
64953b0596d07dbee9a9b521ed61c434ce4709b5Satyen Desai * The base-build submodule provides Base.build functionality, which
64953b0596d07dbee9a9b521ed61c434ce4709b5Satyen Desai * can be used to create custom classes, by aggregating extensions onto
64953b0596d07dbee9a9b521ed61c434ce4709b5Satyen Desai * a main class.
2b449ca06646ebb6603e35918a40bca8e0f43c8eSatyen Desai * @module base
64953b0596d07dbee9a9b521ed61c434ce4709b5Satyen Desai * @submodule base-build
1201815cf554ddd27ca4898d9623926cfe3c2ac9Satyen Desai * The build configuration for the Base class.
64953b0596d07dbee9a9b521ed61c434ce4709b5Satyen Desai * Defines the static fields which need to be aggregated
64953b0596d07dbee9a9b521ed61c434ce4709b5Satyen Desai * when the Base class is used as the main class passed to
793e508bd277c48d6a09b91bb99064b4046eda7aSatyen Desai * the <a href="#method_Base.build">Base.build</a> method.
793e508bd277c48d6a09b91bb99064b4046eda7aSatyen Desai * @property Base._buildCfg
1201815cf554ddd27ca4898d9623926cfe3c2ac9Satyen Desai * @type Object
64953b0596d07dbee9a9b521ed61c434ce4709b5Satyen Desai * Builds a custom constructor function (class) from the
1201815cf554ddd27ca4898d9623926cfe3c2ac9Satyen Desai * main function, and array of extension functions (classes)
64953b0596d07dbee9a9b521ed61c434ce4709b5Satyen Desai * provided. The NAME field for the constructor function is
64953b0596d07dbee9a9b521ed61c434ce4709b5Satyen Desai * defined by the first argument passed in.
eaa9719567e2ec55d96f9298077f5fc0effe1ed3Satyen Desai * The cfg object supports the following properties
1201815cf554ddd27ca4898d9623926cfe3c2ac9Satyen Desai * <dt>dynamic <boolean></dt>
1201815cf554ddd27ca4898d9623926cfe3c2ac9Satyen Desai * <p>If true (default), a completely new class
1201815cf554ddd27ca4898d9623926cfe3c2ac9Satyen Desai * is created which extends the main class, and acts as the
1201815cf554ddd27ca4898d9623926cfe3c2ac9Satyen Desai * host on which the extension classes are augmented.</p>
1201815cf554ddd27ca4898d9623926cfe3c2ac9Satyen Desai * <p>If false, the extensions classes are augmented directly to
64953b0596d07dbee9a9b521ed61c434ce4709b5Satyen Desai * the main class, modifying the main class' prototype.</p>
1201815cf554ddd27ca4898d9623926cfe3c2ac9Satyen Desai * <dt>aggregates <String[]></dt>
1201815cf554ddd27ca4898d9623926cfe3c2ac9Satyen Desai * <dd>An array of static property names, which will get aggregated
1201815cf554ddd27ca4898d9623926cfe3c2ac9Satyen Desai * on to the built class, in addition to the default properties build
793e508bd277c48d6a09b91bb99064b4046eda7aSatyen Desai * will always aggregate as defined by the main class' static _buildCfg
793e508bd277c48d6a09b91bb99064b4046eda7aSatyen Desai * @method Base.build
04f6f082e2978c0be5c674a54883b28e50040c39Satyen Desai * @param {Function} name The name of the new class. Used to defined the NAME property for the new class.
1201815cf554ddd27ca4898d9623926cfe3c2ac9Satyen Desai * @param {Function} main The main class on which to base the built class
1201815cf554ddd27ca4898d9623926cfe3c2ac9Satyen Desai * @param {Function[]} extensions The set of extension classes which will be
1201815cf554ddd27ca4898d9623926cfe3c2ac9Satyen Desai * augmented/aggregated to the built class.
64953b0596d07dbee9a9b521ed61c434ce4709b5Satyen Desai * @param {Object} cfg Optional. Build configuration for the class (see description).
1201815cf554ddd27ca4898d9623926cfe3c2ac9Satyen Desai * @return {Function} A custom class, created from the provided main and extension classes
2b449ca06646ebb6603e35918a40bca8e0f43c8eSatyen Desai Base.build = function(name, main, extensions, cfg) {
1201815cf554ddd27ca4898d9623926cfe3c2ac9Satyen Desai // Shallow isolate aggregates
1201815cf554ddd27ca4898d9623926cfe3c2ac9Satyen Desai for (i = 0, l = aggregates.length; i < l; ++i) {
1201815cf554ddd27ca4898d9623926cfe3c2ac9Satyen Desai builtClass[val] = L.isArray(main[val]) ? [] : {};
1201815cf554ddd27ca4898d9623926cfe3c2ac9Satyen Desai Y.aggregate(builtClass, main, true, aggregates);
1201815cf554ddd27ca4898d9623926cfe3c2ac9Satyen Desai for (i = 0, l = extensions.length; i < l; i++) {
1201815cf554ddd27ca4898d9623926cfe3c2ac9Satyen Desai Y.aggregate(builtClass, extClass, true, aggregates);
1201815cf554ddd27ca4898d9623926cfe3c2ac9Satyen Desai // Old augment
1201815cf554ddd27ca4898d9623926cfe3c2ac9Satyen Desai BuiltClass.superclass.constructor.apply(this, arguments);
1201815cf554ddd27ca4898d9623926cfe3c2ac9Satyen Desai for (i = 0; i < l; i++) {
1201815cf554ddd27ca4898d9623926cfe3c2ac9Satyen Desai return this;
c938b255ef3c02ee132e52fbd15bb211c6f3f760Satyen Desai for (var i = 0, l = classes.length; i < l; i++) {
c938b255ef3c02ee132e52fbd15bb211c6f3f760Satyen Desai return true;
1201815cf554ddd27ca4898d9623926cfe3c2ac9Satyen Desai return false;
1201815cf554ddd27ca4898d9623926cfe3c2ac9Satyen Desai var dynamic = (cfg && false === cfg.dynamic) ? false : true,
2b449ca06646ebb6603e35918a40bca8e0f43c8eSatyen Desai builtClass = (dynamic) ? Base.build._template(main) : main;
1201815cf554ddd27ca4898d9623926cfe3c2ac9Satyen Desai while (c && c.prototype) {
1201815cf554ddd27ca4898d9623926cfe3c2ac9Satyen Desai classAggr = c._buildCfg && c._buildCfg.aggregates;
1201815cf554ddd27ca4898d9623926cfe3c2ac9Satyen Desai c = c.superclass ? c.superclass.constructor : null;