Skip to content

Commit ff6afcf

Browse files
committed
fix: improve bootstrap & added compatibility with karma
Fixes #111
1 parent e3bf1d2 commit ff6afcf

5 files changed

Lines changed: 74 additions & 34 deletions

File tree

examples/complexExample/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<html lang="en" xmlns:ng="http://angularjs.org" ng-app="app">
33
<head>
44
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
5-
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.20/angular.min.js"></script>
5+
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js"></script>
66
<script type="text/javascript" src="bower_components/angular-ui-router/release/angular-ui-router.js"></script>
77
<script type="text/javascript" src="../../src/ocLazyLoad.js"></script>
88
<script type="text/javascript" src="js/app.js"></script>

package.json

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,44 @@
1111
},
1212
"scripts": {
1313
"test": "karma start karma.conf.js",
14+
"test-dev": "karma start karma.conf.js --single-run=false --browsers=Chrome",
1415
"build": "gulp build"
1516
},
1617
"repository": {
1718
"type": "git",
1819
"url": "git://github.com/ocombe/ocLazyLoad.git"
1920
},
20-
"keywords": ["lazy load", "lazy-load", "load on demand", "module", "angular", "angularJS"],
21+
"keywords": [
22+
"lazy load",
23+
"lazy-load",
24+
"load on demand",
25+
"module",
26+
"angular",
27+
"angularJS"
28+
],
2129
"bugs": {
2230
"url": "https://github.com/ocombe/ocLazyLoad/issues"
2331
},
2432
"devDependencies": {
25-
"gulp": "^3.8.6",
26-
"gulp-clean": "^0.3.1",
27-
"gulp-concat": "^2.3.3",
28-
"gulp-exec": "^2.0.1",
29-
"gulp-header": "^1.0.5",
30-
"gulp-json-editor": "^2.0.2",
31-
"gulp-prompt": "^0.1.1",
32-
"gulp-rename": "^1.2.0",
33-
"gulp-uglify": "^0.3.1",
34-
"karma": "^0.12.24",
35-
"karma-as-promised": "^1.0.0",
36-
"karma-chrome-launcher": "^0.1.5",
37-
"karma-cli": "0.0.4",
38-
"karma-coverage": "^0.2.6",
39-
"karma-firefox-launcher": "^0.1.3",
40-
"karma-jasmine": "^0.2.3",
41-
"qq": "^0.3.5",
42-
"requirejs": "^2.1.15",
43-
"semver": "^2.3.1",
44-
"streamqueue": "^0.1.1"
33+
"gulp": "~3.8.10",
34+
"gulp-clean": "~0.3.1",
35+
"gulp-concat": "~2.4.3",
36+
"gulp-exec": "~2.1.1",
37+
"gulp-header": "~1.2.2",
38+
"gulp-json-editor": "~2.2.1",
39+
"gulp-prompt": "~0.1.1",
40+
"gulp-rename": "~1.2.0",
41+
"gulp-uglify": "~1.1.0",
42+
"karma": "~0.12.31",
43+
"karma-as-promised": "~1.0.0",
44+
"karma-chrome-launcher": "~0.1.7",
45+
"karma-cli": "~0.0.4",
46+
"karma-coverage": "~0.2.7",
47+
"karma-firefox-launcher": "~0.1.4",
48+
"karma-jasmine": "~0.3.5",
49+
"qq": "~0.3.5",
50+
"requirejs": "~2.1.15",
51+
"semver": "~4.2.0",
52+
"streamqueue": "~0.1.1"
4553
}
46-
}
54+
}

src/ocLazyLoad.js

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -689,8 +689,11 @@
689689
* @returns {boolean}
690690
*/
691691
function moduleExists(moduleName) {
692+
if(!angular.isString(moduleName)) {
693+
return false;
694+
}
692695
try {
693-
return angular.module(moduleName);
696+
return ngModuleFct(moduleName);
694697
} catch(e) {
695698
if(/No module/.test(e) || (e.message.indexOf('$injector:nomod') > -1)) {
696699
return false;
@@ -700,7 +703,7 @@
700703

701704
function getModule(moduleName) {
702705
try {
703-
return angular.module(moduleName);
706+
return ngModuleFct(moduleName);
704707
} catch(e) {
705708
// this error message really suxx
706709
if(/No module/.test(e) || (e.message.indexOf('$injector:nomod') > -1)) {
@@ -775,7 +778,7 @@
775778
continue;
776779
}
777780
var newModule = regModules.indexOf(moduleName) === -1;
778-
moduleFn = angular.module(moduleName);
781+
moduleFn = ngModuleFct(moduleName);
779782
if(newModule) { // new module
780783
regModules.push(moduleName);
781784
register(providers, moduleFn.requires, params);
@@ -888,6 +891,7 @@
888891
}
889892
});
890893
}
894+
891895
if(initModules.length === 0) {
892896
throw 'No module found during bootstrap, unable to init ocLazyLoad';
893897
}
@@ -909,14 +913,46 @@
909913
angular.forEach(initModules, function(moduleName) {
910914
addReg(moduleName);
911915
});
916+
917+
initModules = []; // reset for next bootstrap
918+
angular.module = ngModuleFct; // restore angular.module
912919
}
913920

914-
var bootstrap = angular.bootstrap;
921+
var bootstrapFct = angular.bootstrap;
915922
angular.bootstrap = function(element, modules, config) {
916923
initModules = modules.slice(); // make a clean copy
917-
return bootstrap(element, modules, config);
924+
return bootstrapFct(element, modules, config);
925+
};
926+
927+
var addToInit = function addToInit(name) {
928+
if(angular.isString(name) && initModules.indexOf(name) === -1) {
929+
initModules.push(name);
930+
}
918931
};
919932

933+
var ngModuleFct = angular.module;
934+
angular.module = function(name, requires, configFn) {
935+
addToInit(name);
936+
return ngModuleFct(name, requires, configFn);
937+
};
938+
939+
// add unit tests support
940+
if((window.jasmine || window.mocha) && angular.isDefined(angular.mock)) {
941+
var ngMockModuleFct = angular.mock.module;
942+
var windowMockModuleFct = window.module;
943+
window.module = angular.mock.module = function(module) {
944+
var moduleFns = Array.prototype.slice.call(arguments, 0);
945+
if (angular.isObject(module) && !angular.isArray(module)) {
946+
angular.forEach(module, function(value, key) {
947+
addToInit(key);
948+
});
949+
} else if(angular.isString(module)) {
950+
addToInit(module);
951+
}
952+
ngMockModuleFct(module);
953+
}
954+
}
955+
920956
// Array.indexOf polyfill for IE8
921957
if(!Array.prototype.indexOf) {
922958
Array.prototype.indexOf = function(searchElement, fromIndex) {

tests/unit/mocks/app.mock.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
// set app1 as the bootstrap module
2-
document.querySelector('html').setAttribute('ng-app','app1');
3-
41
angular.module('app1', ['oc.lazyLoad'])
52
.config(['$ocLazyLoadProvider', function($ocLazyLoadProvider) {
63
$ocLazyLoadProvider.config({

tests/unit/specs/ocLazyLoad.spec.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/*globals describe, beforeEach, afterEach, inject, module, it, expect, angular */
21
describe('Module: oc.lazyLoad', function() {
32
'use strict';
43

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

6867
it('loadedModules should be working', function() {
69-
expect($ocLazyLoad.getModules()).toEqual(['ng', 'app1', 'oc.lazyLoad']);
68+
expect($ocLazyLoad.getModules()).toEqual(['ng', 'app1', 'oc.lazyLoad', 'ngMockE2E']);
7069
});
7170

7271
it('isLoaded should be working', function() {
@@ -145,7 +144,7 @@ describe('Module: oc.lazyLoad', function() {
145144
window.clearInterval(interval);
146145
throw err;
147146
});
148-
})
147+
});
149148

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

0 commit comments

Comments
 (0)