Skip to content

Commit 58232a5

Browse files
committed
auto rewrite config for routeProvider.when or stateProvider.state
1 parent cd25de0 commit 58232a5

File tree

1 file changed

+40
-11
lines changed

1 file changed

+40
-11
lines changed

‎angular-async-loader.js

+40-11
Original file line numberDiff line numberDiff line change
@@ -99,23 +99,52 @@
9999
};
100100
}]);
101101

102+
// rewrite $routeProvider.when
103+
if (app.requires && app.requires.indexOf('ngRoute') !== -1) {
104+
app.config(['$routeProvider', function ($routeProvider) {
105+
var whenFn = $routeProvider.when;
106+
$routeProvider.when = function(path, config) {
107+
return whenFn.call($routeProvider, path, app.route(config));
108+
};
109+
}]);
110+
}
111+
// rewrite $stateProvider.state
112+
if (app.requires && app.requires.indexOf('ui.router') !== -1) {
113+
app.config(['$stateProvider', function ($stateProvider) {
114+
var stateFn = $stateProvider.state;
115+
$stateProvider.state = function(state, config) {
116+
return stateFn.call($stateProvider, state, app.route(config));
117+
};
118+
}]);
119+
}
102120

103121
/**
104-
* Generate $routeProvider.route or $stateProvider.state.
122+
* Rewrite config for $routeProvider.when or $stateProvider.state.
105123
*
106124
* Populate the resolve attribute using either 'controllerUrl'.
107125
*
108-
* @param config {Object}
126+
* @param {Object} config
109127
* @returns the modified config
110128
*/
111129
app.route = function (config) {
112-
var controllerUrl = config.controllerUrl;
113-
if (controllerUrl !== undefined) {
114-
delete config.controllerUrl;
130+
function rewriteViewConfig(config) {
131+
if (config.hasOwnProperty('controllerUrl')) {
132+
var controllerUrl = config.controllerUrl;
133+
delete config.controllerUrl;
134+
135+
var resolve = config.resolve || {};
136+
resolve.dummyController = app.load([controllerUrl]);
137+
config.resolve = resolve;
138+
}
139+
}
115140

116-
var resolve = config.resolve || {};
117-
resolve.dummyController = app.load(controllerUrl);
118-
config.resolve = resolve;
141+
// multiple views support
142+
if (config.hasOwnProperty('views')) {
143+
Object.keys(config.views).forEach(function(view) {
144+
rewriteViewConfig(config.views[view]);
145+
});
146+
} else {
147+
rewriteViewConfig(config);
119148
}
120149

121150
return config;
@@ -126,7 +155,7 @@
126155
* Load external resources, such as Controller, Service, etc.
127156
*
128157
* @param {String|Array} dependencies
129-
* @returns {*}
158+
* @returns {*} a promised function to ajax load resource
130159
*/
131160
app.load = function (dependencies) {
132161
if (!angular.isArray(dependencies)) {
@@ -148,10 +177,10 @@
148177
var injector;
149178

150179
/**
151-
* Get angular injector object by name.
180+
* Get angular injector object by name in module scope.
152181
*
153182
* @param {String} name
154-
* @returns {*}
183+
* @returns {*} the injected object
155184
*/
156185
app.get = function (name) {
157186
if (injector === undefined) {

0 commit comments

Comments
 (0)