Skip to content

Commit

Permalink
fix: don't watch for angular.module calls when you're not lazy loading
Browse files Browse the repository at this point in the history
  • Loading branch information
ocombe committed Feb 28, 2015
1 parent 5fb4e99 commit 35f7eb5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
24 changes: 15 additions & 9 deletions src/ocLazyLoad.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
runBlocks = {},
ocLazyLoad = angular.module('oc.lazyLoad', ['ng']),
broadcast = angular.noop,
modulesToLoad = [];
modulesToLoad = [],
recordDeclarations = [true];

ocLazyLoad.provider('$ocLazyLoad', ['$controllerProvider', '$provide', '$compileProvider', '$filterProvider', '$injector', '$animateProvider',
function($controllerProvider, $provide, $compileProvider, $filterProvider, $injector, $animateProvider) {
Expand Down Expand Up @@ -248,13 +249,15 @@
templatesLoader.ocLazyLoadLoader = true;
}

var filesLoader = function(config, params) {
var filesLoader = function filesLoader(config, params) {
var cssFiles = [],
templatesFiles = [],
jsFiles = [],
promises = [],
cachePromise = null;

recordDeclarations.push(true); // start watching angular.module calls

angular.extend(params || {}, config);

var pushFile = function(path) {
Expand Down Expand Up @@ -349,7 +352,10 @@
return filesLoader(config, params);
});
} else {
return $q.all(promises);
return $q.all(promises).finally(function(res) {
recordDeclarations.pop(); // stop watching angular.module calls
return res;
});
}
};

Expand Down Expand Up @@ -431,7 +437,6 @@
moduleCache = [],
deferredList = [],
deferred = $q.defer(),
moduleName,
errText;

if(angular.isUndefined(params)) {
Expand Down Expand Up @@ -962,6 +967,7 @@
});

modulesToLoad = []; // reset for next bootstrap
recordDeclarations.pop(); // wait for the next lazy load
}

var bootstrapFct = angular.bootstrap;
Expand All @@ -970,15 +976,15 @@
return bootstrapFct(element, modules, config);
};

var addToInit = function addToInit(name) {
if(angular.isString(name) && modulesToLoad.indexOf(name) === -1) {
var addToLoadList = function addToLoadList(name) {
if(recordDeclarations.length > 0 && angular.isString(name) && modulesToLoad.indexOf(name) === -1) {
modulesToLoad.push(name);
}
};

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

Expand All @@ -990,10 +996,10 @@
var moduleFns = Array.prototype.slice.call(arguments, 0);
if (angular.isObject(module) && !angular.isArray(module)) {
angular.forEach(module, function(value, key) {
addToInit(key);
addToLoadList(key);
});
} else if(angular.isString(module)) {
addToInit(module);
addToLoadList(module);
}
ngMockModuleFct(module);
}
Expand Down
7 changes: 5 additions & 2 deletions tests/unit/lazyLoad/testModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,21 @@ testModule.controller('TestCtrl', function($scope) {
spy.ctrl('ctrl');
});

//
// services
testModule.factory('testService', [function () {
spy.service('service');
return {};
}]);

testModule.filter('testFilter', function () {
// filters
angular.module('testModule').filter('testFilter', function () {
spy.filter('filter');
return function (input) {
return input;
}
});

// directives
testModule.directive("test", function () {
spy.directive('directive');
return {
Expand All @@ -56,6 +58,7 @@ testModule.directive("test", function () {
};
});

// constants
testModule.constant("myConst1", "something");
testModule.constant("myConst2", undefined);
testModule.constant("myConst3", null);

0 comments on commit 35f7eb5

Please sign in to comment.