Skip to content

Commit

Permalink
fix: improve bootstrap & added compatibility with karma
Browse files Browse the repository at this point in the history
Fixes #111
  • Loading branch information
ocombe committed Jan 20, 2015
1 parent e3bf1d2 commit ff6afcf
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 34 deletions.
2 changes: 1 addition & 1 deletion examples/complexExample/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html lang="en" xmlns:ng="http://angularjs.org" ng-app="app">
<head>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.20/angular.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js"></script>
<script type="text/javascript" src="bower_components/angular-ui-router/release/angular-ui-router.js"></script>
<script type="text/javascript" src="../../src/ocLazyLoad.js"></script>
<script type="text/javascript" src="js/app.js"></script>
Expand Down
52 changes: 30 additions & 22 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,44 @@
},
"scripts": {
"test": "karma start karma.conf.js",
"test-dev": "karma start karma.conf.js --single-run=false --browsers=Chrome",
"build": "gulp build"
},
"repository": {
"type": "git",
"url": "git://github.com/ocombe/ocLazyLoad.git"
},
"keywords": ["lazy load", "lazy-load", "load on demand", "module", "angular", "angularJS"],
"keywords": [
"lazy load",
"lazy-load",
"load on demand",
"module",
"angular",
"angularJS"
],
"bugs": {
"url": "https://github.com/ocombe/ocLazyLoad/issues"
},
"devDependencies": {
"gulp": "^3.8.6",
"gulp-clean": "^0.3.1",
"gulp-concat": "^2.3.3",
"gulp-exec": "^2.0.1",
"gulp-header": "^1.0.5",
"gulp-json-editor": "^2.0.2",
"gulp-prompt": "^0.1.1",
"gulp-rename": "^1.2.0",
"gulp-uglify": "^0.3.1",
"karma": "^0.12.24",
"karma-as-promised": "^1.0.0",
"karma-chrome-launcher": "^0.1.5",
"karma-cli": "0.0.4",
"karma-coverage": "^0.2.6",
"karma-firefox-launcher": "^0.1.3",
"karma-jasmine": "^0.2.3",
"qq": "^0.3.5",
"requirejs": "^2.1.15",
"semver": "^2.3.1",
"streamqueue": "^0.1.1"
"gulp": "~3.8.10",
"gulp-clean": "~0.3.1",
"gulp-concat": "~2.4.3",
"gulp-exec": "~2.1.1",
"gulp-header": "~1.2.2",
"gulp-json-editor": "~2.2.1",
"gulp-prompt": "~0.1.1",
"gulp-rename": "~1.2.0",
"gulp-uglify": "~1.1.0",
"karma": "~0.12.31",
"karma-as-promised": "~1.0.0",
"karma-chrome-launcher": "~0.1.7",
"karma-cli": "~0.0.4",
"karma-coverage": "~0.2.7",
"karma-firefox-launcher": "~0.1.4",
"karma-jasmine": "~0.3.5",
"qq": "~0.3.5",
"requirejs": "~2.1.15",
"semver": "~4.2.0",
"streamqueue": "~0.1.1"
}
}
}
46 changes: 41 additions & 5 deletions src/ocLazyLoad.js
Original file line number Diff line number Diff line change
Expand Up @@ -689,8 +689,11 @@
* @returns {boolean}
*/
function moduleExists(moduleName) {
if(!angular.isString(moduleName)) {
return false;
}
try {
return angular.module(moduleName);
return ngModuleFct(moduleName);
} catch(e) {
if(/No module/.test(e) || (e.message.indexOf('$injector:nomod') > -1)) {
return false;
Expand All @@ -700,7 +703,7 @@

function getModule(moduleName) {
try {
return angular.module(moduleName);
return ngModuleFct(moduleName);
} catch(e) {
// this error message really suxx
if(/No module/.test(e) || (e.message.indexOf('$injector:nomod') > -1)) {
Expand Down Expand Up @@ -775,7 +778,7 @@
continue;
}
var newModule = regModules.indexOf(moduleName) === -1;
moduleFn = angular.module(moduleName);
moduleFn = ngModuleFct(moduleName);
if(newModule) { // new module
regModules.push(moduleName);
register(providers, moduleFn.requires, params);
Expand Down Expand Up @@ -888,6 +891,7 @@
}
});
}

if(initModules.length === 0) {
throw 'No module found during bootstrap, unable to init ocLazyLoad';
}
Expand All @@ -909,14 +913,46 @@
angular.forEach(initModules, function(moduleName) {
addReg(moduleName);
});

initModules = []; // reset for next bootstrap
angular.module = ngModuleFct; // restore angular.module
}

var bootstrap = angular.bootstrap;
var bootstrapFct = angular.bootstrap;
angular.bootstrap = function(element, modules, config) {
initModules = modules.slice(); // make a clean copy
return bootstrap(element, modules, config);
return bootstrapFct(element, modules, config);
};

var addToInit = function addToInit(name) {
if(angular.isString(name) && initModules.indexOf(name) === -1) {
initModules.push(name);
}
};

var ngModuleFct = angular.module;
angular.module = function(name, requires, configFn) {
addToInit(name);
return ngModuleFct(name, requires, configFn);
};

// add unit tests support
if((window.jasmine || window.mocha) && angular.isDefined(angular.mock)) {
var ngMockModuleFct = angular.mock.module;
var windowMockModuleFct = window.module;
window.module = angular.mock.module = function(module) {
var moduleFns = Array.prototype.slice.call(arguments, 0);
if (angular.isObject(module) && !angular.isArray(module)) {
angular.forEach(module, function(value, key) {
addToInit(key);
});
} else if(angular.isString(module)) {
addToInit(module);
}
ngMockModuleFct(module);
}
}

// Array.indexOf polyfill for IE8
if(!Array.prototype.indexOf) {
Array.prototype.indexOf = function(searchElement, fromIndex) {
Expand Down
3 changes: 0 additions & 3 deletions tests/unit/mocks/app.mock.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// set app1 as the bootstrap module
document.querySelector('html').setAttribute('ng-app','app1');

angular.module('app1', ['oc.lazyLoad'])
.config(['$ocLazyLoadProvider', function($ocLazyLoadProvider) {
$ocLazyLoadProvider.config({
Expand Down
5 changes: 2 additions & 3 deletions tests/unit/specs/ocLazyLoad.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/*globals describe, beforeEach, afterEach, inject, module, it, expect, angular */
describe('Module: oc.lazyLoad', function() {
'use strict';

Expand Down Expand Up @@ -66,7 +65,7 @@ describe('Module: oc.lazyLoad', function() {
});

it('loadedModules should be working', function() {
expect($ocLazyLoad.getModules()).toEqual(['ng', 'app1', 'oc.lazyLoad']);
expect($ocLazyLoad.getModules()).toEqual(['ng', 'app1', 'oc.lazyLoad', 'ngMockE2E']);
});

it('isLoaded should be working', function() {
Expand Down Expand Up @@ -145,7 +144,7 @@ describe('Module: oc.lazyLoad', function() {
window.clearInterval(interval);
throw err;
});
})
});

it('should be able to execute config blocks', function() {
expect(window.spy.config).toHaveBeenCalledWith('config1');
Expand Down

0 comments on commit ff6afcf

Please sign in to comment.