Kevnz
9/11/2013 - 9:51 AM

Adds App.create to allow for mixing in multiple apps.

Adds App.create to allow for mixing in multiple apps.

YUI().use('first-app', 'second-app', 'app-extend', 'simple-view', function (Y) {
    Y.log(Y.App);

  Y.MainApp = Y.App.create('mainApp', Y.App, [Y.FirstApp, Y.SecondApp], {
    views: {
        main: {
            type: Y.SimpleView
        }
    },
    showMain: function (req, res, next) {
      console.log('show main');
      this.showView('main', {template: '#main-template', message: 'Main App and Main Route' });
    },
    initializer: function () {
        Y.log('init of Main App');
        var self = this;
    }
  }, {
    ATTRS: {
      routes: {
        value: [
          { path:'/', callbacks:'showMain' }
        ]
      }
    }
  });


  var app = new Y.MainApp({container:'#app'});

  app.render();
  app.save('/');
});
YUI.add('app-create', function (Y) {
var Base = Y.Base,
    INITIALIZER = "initializer",
    DESTRUCTOR  = "destructor",
    VIEWS = "views",
    build = Base._build,
    _build = function(name, main, extensions, px, sx, cfg) {



        var builtClass = build._ctor(main, cfg),
        buildCfg = build._cfg(main, cfg, extensions),

        _mixCust = build._mixCust,

        dynamic = builtClass._yuibuild.dynamic,

        i, l, extClass, extProto,
        initializer,
        destructor,
        views,
        builtViews = builtClass.prototype.views;

        var builtRoutes = [];


        // Augment/Aggregate
        for (i = 0, l = extensions.length; i < l; i++) {
            extClass = extensions[i];

            extProto = extClass.prototype;

            initializer = extProto[INITIALIZER];
            destructor = extProto[DESTRUCTOR];
            views = extProto[VIEWS];

            delete extProto[VIEWS];
            delete extProto[INITIALIZER];
            delete extProto[DESTRUCTOR];

            // Prototype, old non-displacing augment
            Y.mix(builtClass, extClass, true, null, 1);
            //Y.mix(builtViews, views, true, null, 1);
            builtViews  = Y.merge(builtViews, views);

            // Custom Statics
            _mixCust(builtClass, extClass, buildCfg);

            if (initializer) {
                extProto[INITIALIZER] = initializer;
            }

            if (destructor) {
                extProto[DESTRUCTOR] = destructor;
            }

            builtRoutes = builtRoutes.concat(extClass.ATTRS.routes.value);

            builtClass._yuibuild.exts.push(extClass);
        }
    
    if (px) {
        Y.mix(builtClass.prototype, px, true);
    }

    if (sx) {
        sx.ATTRS.routes.value = builtRoutes.concat(sx.ATTRS.routes.value);
        Y.mix(builtClass, build._clean(sx, buildCfg), true);
        _mixCust(builtClass, sx, buildCfg);
    }

    builtClass.prototype.hasImpl = build._impl;
    var newViews = Y.merge(builtClass.prototype.views, builtViews);
    builtClass.prototype.views = newViews;
    if (dynamic) {
        builtClass.NAME = name;
        builtClass.prototype.constructor = builtClass;

        // Carry along the reference to `modifyAttrs()` from `main`.
        builtClass.modifyAttrs = main.modifyAttrs;
    }
    return builtClass;
};

var AppCreate = function (config) {

};
AppCreate.NAME = 'app-create';

AppCreate.create = function(name, base, extensions, px, sx) {
    return _build(name, base, extensions, px, sx);
};

Y.App.Create = AppCreate;
Y.App.create = AppCreate.create;
Y.Base.mix(Y.App, [AppCreate]);
}, '0.0.0', { requires:['base-build']});