Skip to content

Commit

Permalink
1.2.1 released
Browse files Browse the repository at this point in the history
  • Loading branch information
subchen committed Nov 6, 2015
1 parent 2648470 commit 2bd71ef
Show file tree
Hide file tree
Showing 7 changed files with 252 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ require.config({
paths: {
'angular': 'assets/angular/angular.min',
'angular-ui-router': 'assets/angular-ui-router/release/angular-ui-router.min',
'angular-async-loader': 'assets/angular-async-loader/angular-async-loader'
'angular-async-loader': 'assets/angular-async-loader/dist/angular-async-loader.min'
},
shim: {
'angular': {exports: 'angular'},
Expand Down
11 changes: 5 additions & 6 deletions angular-async-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

function factory(angular, undefined) {

var VERSION = '1.2.0';
var VERSION = '1.2.1';

// Support require.js, sea.js, system.js
var amdRequire = (function() {
Expand All @@ -34,15 +34,14 @@
* @param {String|Array} dependencies
* @returns {*} a promised function to ajax load dependencies
*/
function requireLoadFn(dependencies) {
function resolveDependencies(dependencies) {
if (typeof(dependencies) === 'string') {
dependencies = [dependencies];
}
return ['$q', '$rootScope', function($q, $rootScope) {
return ['$q', function($q) {
var defer = $q.defer();
amdRequire(dependencies, function() {
defer.resolve(Array.prototype.slice.call(arguments));
$rootScope.$apply();
defer.resolve(arguments);
});
return defer.promise;
}];
Expand Down Expand Up @@ -74,7 +73,7 @@
delete config.controllerUrl;

var resolve = config.resolve || {};
resolve['$dummy'] = requireLoadFn(dependencies);
resolve['$dummy'] = resolveDependencies(dependencies);
config.resolve = resolve;
}
}
Expand Down
235 changes: 235 additions & 0 deletions dist/angular-async-loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
/**
* async loader for angular 1.x.
*
* https://github.com/subchen/angular-async-loader
*
* subchen@gmail.com
*/
(function() {

function factory(angular, undefined) {

var VERSION = '1.2.1';

// Support require.js, sea.js, system.js
var amdRequire = (function() {
if (typeof(require) === 'function') {
if (typeof(require.async) === 'function') {
return require.async;
} else {
return require;
}
} else if (typeof(seajs) === 'object' && typeof(seajs.use) === 'function') {
return seajs.use;
} else if (typeof(System) === 'object' && typeof(System.amdRequire) === 'function') {
return System.amdRequire;
}
throw new Error('No amd/cmd module loader found.');
}());

/**
* Load external dependencies, such as Controller, Service, etc.
*
* @private
* @param {String|Array} dependencies
* @returns {*} a promised function to ajax load dependencies
*/
function resolveDependencies(dependencies) {
if (typeof(dependencies) === 'string') {
dependencies = [dependencies];
}
return ['$q', function($q) {
var defer = $q.defer();
amdRequire(dependencies, function() {
defer.resolve(arguments);
});
return defer.promise;
}];
}

/**
* Rewrite route config for $routeProvider.when or $stateProvider.state.
*
* Transform 'controllerUrl' and 'dependencies' attrs into resolve object.
*
* @private
* @param {Object} config
* @returns {Object} the modified config
*/
function route(config) {

function rewriteConfig(config) {
if (config.hasOwnProperty('controllerUrl') || config.hasOwnProperty('dependencies')) {
var dependencies = config.dependencies;
if (dependencies === undefined) {
dependencies = [];
} else if (typeof(dependencies) === 'string') {
dependencies = [dependencies];
}
if (config.controllerUrl) {
dependencies.push(config.controllerUrl);
}
delete config.dependencies;
delete config.controllerUrl;

var resolve = config.resolve || {};
resolve['$dummy'] = resolveDependencies(dependencies);
config.resolve = resolve;
}
}

// multiple views support
if (config.hasOwnProperty('views')) {
Object.keys(config.views).forEach(function(view) {
rewriteConfig(config.views[view]);
});
} else {
rewriteConfig(config);
}

return config;
}


return {
/**
* Version of npm package.
*/
VERSION: VERSION,

/**
* Configure angular module instance to support async load components.
*
* @param {angular.Module} app
*/
configure: function(app) {

app.provider('$asyncLoader', [
'$controllerProvider',
'$compileProvider',
'$provide',
'$filterProvider',
function($controllerProvider,
$compileProvider,
$provide,
$filterProvider) {
this.$get = function() {
return {
$controllerProvider: $controllerProvider,
$compileProvider: $compileProvider,
$provide: $provide,
$filterProvider: $filterProvider
};
};
}
]);

app.run(['$asyncLoader', function($asyncLoader) {
var $controllerProvider = $asyncLoader.$controllerProvider;
var $compileProvider = $asyncLoader.$compileProvider;
var $provide = $asyncLoader.$provide;
var $filterProvider = $asyncLoader.$filterProvider;

app.value = function(name, value) {
$provide.value(name, value);
return app;
};

app.constant = function(name, value) {
$provide.constant(name, value);
return app;
};

app.factory = function(name, factory) {
$provide.factory(name, factory);
return app;
};

app.service = function(name, service) {
$provide.service(name, service);
return app;
};

app.filter = function(name, filter) {
$filterProvider.register(name, filter);
return app;
};

app.directive = function(name, directive) {
$compileProvider.directive(name, directive);
return app;
};

app.controller = function(name, controller) {
$controllerProvider.register(name, controller);
return app;
};

app.decorator = function(name, decorator) {
$provide.decorator(name, decorator);
return app;
};

app.provider = function(name, service) {
$provide.provider(name, service);
return app;
};
}]);

// 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));
};
}]);
}


var injector;

/**
* Get angular injector object by name in module scope.
*
* @param {String} name
* @returns {*} the injected object
*/
app.get = function(name) {
if (injector === undefined) {
var elements = [app.element, document, 'html', 'body'];
for (var i = 0; i < elements.length; i++) {
injector = angular.element(elements[i]).injector();
if (injector !== undefined) {
break;
}
}
}
return injector.get(name);
};

}
};
}

/**
* @exports
*/
if (typeof(define) === 'function' && define.amd) {
define(['angular'], function(angular) {
return factory(angular);
});
} else {
window.asyncLoader = factory(window.angular);
}

}());
2 changes: 2 additions & 0 deletions dist/angular-async-loader.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dist/angular-async-loader.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-async-loader",
"version": "1.2.0",
"version": "1.2.1",
"description": "async loader for angular 1.x",
"author": {
"name": "Guoqiang Chen",
Expand All @@ -25,6 +25,11 @@
"ui-router",
"ngRoute"
],
"dependencies": {
"dependencies": {},
"devDependencies": {
"uglifyjs": "^2.4.10"
},
"scripts": {
"dist": "cd ./dist; cp ../angular-async-loader.js ./; uglifyjs angular-async-loader.js -c -m -o angular-async-loader.min.js --source-map angular-async-loader.min.js.map"
}
}
2 changes: 1 addition & 1 deletion sample/webapp/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ require.config({
paths: {
'angular': 'assets/angular/angular.min',
'angular-ui-router': 'assets/angular-ui-router/release/angular-ui-router.min',
'angular-async-loader': 'assets/angular-async-loader/angular-async-loader'
'angular-async-loader': 'assets/angular-async-loader/dist/angular-async-loader.min'
},
shim: {
'angular': {exports: 'angular'},
Expand Down

0 comments on commit 2bd71ef

Please sign in to comment.