Skip to content

Master #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 68 additions & 31 deletions angular-async-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,26 +133,49 @@
* @param {String} name - module name
*/
app.useModule = function (name) {
var _runBlocks = [];

var module = angular.module(name);

if (module.requires) {
for (var i = 0; i < module.requires.length; i++) {
app.useModule(module.requires[i]);
}
}
angular.forEach(module._invokeQueue, function(args) {
var provider = ngProviders[args[0]] || $injector.get(args[0]);
provider[args[1]].apply(provider, args[2]);
});
angular.forEach(module._configBlocks, function(args) {
var provider = ngProviders.$injector.get(args[0]);
provider[args[1]].apply(provider, args[2]);
});
angular.forEach(module._runBlocks, function(args) {
processInvokeQueueAndConfigBlocks(module);

/**
* when all modules has loaded, run _runBlocks
*/
angular.forEach(_runBlocks, function (args) {
$injector.invoke(args);
});

return app;

function processInvokeQueueAndConfigBlocks(module) {

if (module.requires.length > 0) {
for (var i = 0; i < module.requires.length; i++) {
processInvokeQueueAndConfigBlocks(module.requires[i]);
}
}
/**
* register providers
*/
angular.forEach(module._invokeQueue, function (args) {
//var provider = ngProviders[args[0]] || $injector.get(args[0]);
var provider = ngProviders.$injector.get(args[0]);
provider[args[1]].apply(provider, args[2]);
});
/**
* config providers
*/
angular.forEach(module._configBlocks, function (args) {
var provider = ngProviders.$injector.get(args[0]);
provider[args[1]].apply(provider, args[2]);
});

/**
* concat all runBlocks
*/
[].push.apply(_runBlocks, module._runBlocks);
}
};

app.value = function(name, value) {
Expand Down Expand Up @@ -212,24 +235,38 @@

}]);

// rewrite $routeProvider.when
if (app.requires && app.requires.indexOf('ngRoute') !== -1) {
app.config(['$routeProvider', function($routeProvider) {
var whenFn = $routeProvider.when;
$routeProvider.when = function(path, config) {
return whenFn.call($routeProvider, path, route(config));
};
}]);
}
// rewrite $stateProvider.state
if (app.requires && app.requires.indexOf('ui.router') !== -1) {
app.config(['$stateProvider', function($stateProvider) {
var stateFn = $stateProvider.state;
$stateProvider.state = function(state, config) {
return stateFn.call($stateProvider, state, route(config));
};
}]);

// rewrite router
function rewriteRoute(requires) {
if (requires.length == 0) {
return;
}
for (var i = 0; i < requires.length; i++) {

//rewrite $routeProvider.when
if (requires[i] === 'ui.router') {
app.config(['$stateProvider', function ($stateProvider) {
var stateFn = $stateProvider.state;
$stateProvider.state = function (state, config) {
return stateFn.call($stateProvider, state, route(config));
};
}]);
}

// rewrite $stateProvider.state
if (requires[i] === 'ngRoute') {
app.config(['$routeProvider', function ($routeProvider) {
var whenFn = $routeProvider.when;
$routeProvider.when = function (path, config) {
return whenFn.call($routeProvider, path, route(config));
};
}]);
}
rewriteRoute(app.module(requires[i]).requires);

}
}
rewriteRoute(app.requires);
}
};
}
Expand Down