Skip to content

Commit e2fec59

Browse files
author
Olivier Combe
committed
fix: don't execute config blocks multiple times by default
A new option has been added, you can use `reconfig: true` if you want to execute config block at each load of the module Fixes #43 Fixes #41
1 parent 6045214 commit e2fec59

2 files changed

Lines changed: 67 additions & 15 deletions

File tree

README.md

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,58 @@ You can put more than one template script in your template file, just make sure
102102
</script>
103103
```
104104

105-
The load service comes with a second optional parameter that you can use if you need to define some configuration for the requests (check: https://docs.angularjs.org/api/ng/service/$http#usage).
106-
The parameter `cache: false` works for all native loaders (**all requests are cached by default**), other parameters only works with the templates for now (js and css default loaders don't use `$http`).
105+
There is two ways to define config options for the load function. You can use a second optional parameter that will define configs for all the modules that you will load, or you can define optional parameters to each module.
106+
For example, those are equivalents:
107+
```js
108+
$ocLazyLoad.load([{
109+
name: 'TestModule',
110+
files: ['testModule.js', 'bower_components/bootstrap/dist/js/bootstrap.js'],
111+
cache: false
112+
},{
113+
name: 'AnotherModule',
114+
files: ['anotherModule.js']
115+
cache: false
116+
}]);
117+
```
118+
And
119+
```js
120+
$ocLazyLoad.load([{
121+
name: 'TestModule',
122+
files: ['testModule.js', 'bower_components/bootstrap/dist/js/bootstrap.js']
123+
},{
124+
name: 'AnotherModule',
125+
files: ['anotherModule.js']
126+
}],
127+
{cache: false});
128+
```
129+
130+
If you load a template with the native template loader, you can use any parameter from the $http service (check: https://docs.angularjs.org/api/ng/service/$http#usage).
107131
```js
108132
$ocLazyLoad.load(
109133
['partials/template1.html', 'partials/template2.html'],
110134
{cache: false, timeout: 5000}
111135
);
112136
```
113137

138+
The existing parameters that you can use are `cache` and `reconfig`.
139+
The parameter `cache: false` works for all native loaders (**all requests are cached by default**):
140+
141+
```js
142+
$ocLazyLoad.load({
143+
name: 'TestModule',
144+
cache: false,
145+
files: ['testModule.js', 'bower_components/bootstrap/dist/js/bootstrap.js']
146+
});
147+
```
148+
149+
By default, if you reload a module, the config block won't be invoked again (because often it will lead to unexpected results). But if you really need to execute the config function again, use the parameter `reconfig: true`:
150+
```js
151+
$ocLazyLoad.load({
152+
name: 'TestModule',
153+
reconfig: true,
154+
files: ['testModule.js', 'bower_components/bootstrap/dist/js/bootstrap.js']
155+
});
156+
```
114157

115158
### Directive
116159
The directive is very similar to the service. Use the same parameters:

src/ocLazyLoad.js

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
'use strict';
33
var regModules = ['ng'],
44
regInvokes = [],
5+
regConfigs = [],
56
ocLazyLoad = angular.module('oc.lazyLoad', ['ng']),
67
broadcast = angular.noop;
78

@@ -189,13 +190,15 @@
189190
templatesLoader.ocLazyLoadLoader = true;
190191
}
191192

192-
var filesLoader = function(paths, params) {
193+
var filesLoader = function(config, params) {
193194
var cssFiles = [],
194195
templatesFiles = [],
195196
jsFiles = [],
196197
promises = [];
197198

198-
angular.forEach(paths, function(path) {
199+
angular.extend(params || {}, config);
200+
201+
angular.forEach(params.files, function(path) {
199202
if(angular.isUndefined(filesCache.get(path)) || params.cache === false) {
200203
if(/\.css[^\.]*$/.test(path) && cssFiles.indexOf(path) === -1) {
201204
cssFiles.push(path);
@@ -268,7 +271,7 @@
268271

269272
// deprecated
270273
loadTemplateFile: function(paths, params) {
271-
return filesLoader(paths, params);
274+
return filesLoader({files: paths}, params);
272275
},
273276

274277
load: function(module, params) {
@@ -394,7 +397,9 @@
394397
});
395398
if (diff.length !== 0) {
396399
$log.warn('Module "', moduleName, '" attempted to redefine configuration for dependency. "', requireEntry.name, '"\n Additional Files Loaded:', diff);
397-
promisesList.push(filesLoader(diff, params).then(function () {
400+
var c = angular.copy(requireEntry);
401+
c.files = diff;
402+
promisesList.push(filesLoader(c, params).then(function () {
398403
return loadDependencies(requireEntry);
399404
}));
400405
}
@@ -420,7 +425,7 @@
420425
// Check if the dependency has any files that need to be loaded. If there are, push a new promise to the promise list.
421426
if(requireEntry.hasOwnProperty('files') && requireEntry.files.length !== 0) {
422427
if(requireEntry.files) {
423-
promisesList.push(filesLoader(requireEntry.files, params).then(function() {
428+
promisesList.push(filesLoader(requireEntry, params).then(function() {
424429
return loadDependencies(requireEntry)
425430
}));
426431
}
@@ -431,14 +436,14 @@
431436
return $q.all(promisesList);
432437
}
433438

434-
filesLoader(config.files, params).then(function success() {
439+
filesLoader(config, params).then(function success() {
435440
if(moduleName === null) {
436441
deferred.resolve(module);
437442
} else {
438443
moduleCache.push(moduleName);
439444
loadDependencies(moduleName).then(function success() {
440445
try {
441-
register(providers, moduleCache);
446+
register(providers, moduleCache, params);
442447
} catch(e) {
443448
$log.error(e.message);
444449
deferred.reject(e);
@@ -624,7 +629,7 @@
624629
}
625630
}
626631

627-
function invokeQueue(providers, queue) {
632+
function invokeQueue(providers, queue, moduleName, reconfig) {
628633
if(!queue) {
629634
return;
630635
}
@@ -638,7 +643,11 @@
638643
} else {
639644
throw new Error('unsupported provider ' + args[0]);
640645
}
641-
if(registerInvokeList(args[2][0])) {
646+
var invoked = regConfigs.indexOf(moduleName);
647+
if(registerInvokeList(args[2][0]) && (args[1] !== 'invoke' || (args[1] === 'invoke' && (!invoked || reconfig)))) {
648+
if(!invoked) {
649+
regConfigs.push(moduleName);
650+
}
642651
provider[args[1]].apply(provider, args[2]);
643652
}
644653
}
@@ -651,7 +660,7 @@
651660
* @param registerModules
652661
* @returns {*}
653662
*/
654-
function register(providers, registerModules) {
663+
function register(providers, registerModules, params) {
655664
if(registerModules) {
656665
var k, moduleName, moduleFn, runBlocks = [];
657666
for(k = registerModules.length - 1; k >= 0; k--) {
@@ -665,11 +674,11 @@
665674
moduleFn = angular.module(moduleName);
666675
if(regModules.indexOf(moduleName) === -1) { // new module
667676
regModules.push(moduleName);
668-
register(providers, moduleFn.requires);
677+
register(providers, moduleFn.requires, params);
669678
runBlocks = runBlocks.concat(moduleFn._runBlocks);
670679
}
671-
invokeQueue(providers, moduleFn._invokeQueue);
672-
invokeQueue(providers, moduleFn._configBlocks);
680+
invokeQueue(providers, moduleFn._invokeQueue, moduleName, params.reconfig);
681+
invokeQueue(providers, moduleFn._configBlocks, moduleName, params.reconfig); // angular 1.3+
673682
broadcast('ocLazyLoad.moduleLoaded', moduleName);
674683
registerModules.pop();
675684
}

0 commit comments

Comments
 (0)