3
YUI.add('app-subapp-extension', function(Y) {
6
Adds the ability to register sub applications with a parent Y.App instance by
7
recording all instances in a subApps attribute and augmenting the parent
8
routes with the sub applications.
10
@class SubAppRegistration
13
function SubAppRegistration() {}
15
SubAppRegistration.ATTRS = {
21
SubAppRegistration.prototype = {
24
A list of sub applications to be instantiated after initialization
32
@property subApplications
38
Adds all of the sub applications listed in the subApplications property.
40
@method addSubApplications
42
addSubApplications: function() {
43
this.addSubApps(this.subApplications);
47
Adds the sub application and its routes to the parent application.
50
@param {object} SubApp instantiable Y.SubApp object.
51
@param {object} config configuration properties for the subapp.
53
addSubApp: function(SubApp, config) {
54
var subApps = this.get('subApps'),
57
SubApp = new SubApp(config);
58
subApps[SubApp.get('urlNamespace')] = SubApp;
60
routes = this._extractRoutes(SubApp);
61
this._augmentRoutes(routes);
65
Wrapper for addSubApp to add multiple sub apps at once.
68
type: 'path.to.subapplication'
73
@param {array} subApps an array of sub app objects and configs.
75
addSubApps: function(subApps) {
76
Y.Array.each(subApps, function(subApp) {
77
this.addSubApp(subApp.type, subApp.config);
82
Extract the routes out of the sub apps.
84
@method _extractRoutes
86
@param {object | integer | undefined} subApp will extract the routes
87
out of the supplied subApp, index, or all subApps if undefined.
88
@return {array} array of sub app route data.
90
_extractRoutes: function(subApp) {
91
var subApps = this.get('subApps'),
92
routes, subRoutes, i, j;
94
switch (typeof subApp) {
95
case 'number': // If an index is passed in grab that subapp
96
subApp = subApps[subApp];
98
case 'object': // If subapp is passed in fetch it's routes
99
routes = subApp.getSubAppRoutes();
103
for (i = 0; i < subApps.length; i += 1) {
104
subRoutes = subApps[i].getSubAppRoutes();
105
for (j = 0; j < subRoutes.length; j += 1) {
106
routes.push(subRoutes[j]);
111
throw 'subApp valid types are integer, object, or undefined';
117
Adds the sub app routes to the parent routes after the middleware
119
@method _augmentRoutes
121
@param {array} array of route objects.
123
_augmentRoutes: function(routes) {
124
var parentRoutes = this.get('routes'),
127
Y.Array.some(parentRoutes, function(value, index, array) {
128
if (value.path !== '*') {
129
middlewareIndex = index;
134
routes.unshift(middlewareIndex, 0);
135
Array.prototype.splice.apply(parentRoutes, routes);
137
this.set('routes', parentRoutes);
142
Y.namespace('juju').SubAppRegistration = SubAppRegistration;