Skip to content

Commit

Permalink
fix: don't execute config blocks multiple times by default
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ocombe committed Jul 23, 2014
1 parent 6045214 commit e2fec59
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 15 deletions.
47 changes: 45 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,58 @@ You can put more than one template script in your template file, just make sure
</script>
```

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).
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`).
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.
For example, those are equivalents:
```js
$ocLazyLoad.load([{
name: 'TestModule',
files: ['testModule.js', 'bower_components/bootstrap/dist/js/bootstrap.js'],
cache: false
},{
name: 'AnotherModule',
files: ['anotherModule.js']
cache: false
}]);
```
And
```js
$ocLazyLoad.load([{
name: 'TestModule',
files: ['testModule.js', 'bower_components/bootstrap/dist/js/bootstrap.js']
},{
name: 'AnotherModule',
files: ['anotherModule.js']
}],
{cache: false});
```

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).
```js
$ocLazyLoad.load(
['partials/template1.html', 'partials/template2.html'],
{cache: false, timeout: 5000}
);
```

The existing parameters that you can use are `cache` and `reconfig`.
The parameter `cache: false` works for all native loaders (**all requests are cached by default**):

```js
$ocLazyLoad.load({
name: 'TestModule',
cache: false,
files: ['testModule.js', 'bower_components/bootstrap/dist/js/bootstrap.js']
});
```

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`:
```js
$ocLazyLoad.load({
name: 'TestModule',
reconfig: true,
files: ['testModule.js', 'bower_components/bootstrap/dist/js/bootstrap.js']
});
```

### Directive
The directive is very similar to the service. Use the same parameters:
Expand Down
35 changes: 22 additions & 13 deletions src/ocLazyLoad.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
'use strict';
var regModules = ['ng'],
regInvokes = [],
regConfigs = [],
ocLazyLoad = angular.module('oc.lazyLoad', ['ng']),
broadcast = angular.noop;

Expand Down Expand Up @@ -189,13 +190,15 @@
templatesLoader.ocLazyLoadLoader = true;
}

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

angular.forEach(paths, function(path) {
angular.extend(params || {}, config);

angular.forEach(params.files, function(path) {
if(angular.isUndefined(filesCache.get(path)) || params.cache === false) {
if(/\.css[^\.]*$/.test(path) && cssFiles.indexOf(path) === -1) {
cssFiles.push(path);
Expand Down Expand Up @@ -268,7 +271,7 @@

// deprecated
loadTemplateFile: function(paths, params) {
return filesLoader(paths, params);
return filesLoader({files: paths}, params);
},

load: function(module, params) {
Expand Down Expand Up @@ -394,7 +397,9 @@
});
if (diff.length !== 0) {
$log.warn('Module "', moduleName, '" attempted to redefine configuration for dependency. "', requireEntry.name, '"\n Additional Files Loaded:', diff);
promisesList.push(filesLoader(diff, params).then(function () {
var c = angular.copy(requireEntry);
c.files = diff;
promisesList.push(filesLoader(c, params).then(function () {
return loadDependencies(requireEntry);
}));
}
Expand All @@ -420,7 +425,7 @@
// Check if the dependency has any files that need to be loaded. If there are, push a new promise to the promise list.
if(requireEntry.hasOwnProperty('files') && requireEntry.files.length !== 0) {
if(requireEntry.files) {
promisesList.push(filesLoader(requireEntry.files, params).then(function() {
promisesList.push(filesLoader(requireEntry, params).then(function() {
return loadDependencies(requireEntry)
}));
}
Expand All @@ -431,14 +436,14 @@
return $q.all(promisesList);
}

filesLoader(config.files, params).then(function success() {
filesLoader(config, params).then(function success() {
if(moduleName === null) {
deferred.resolve(module);
} else {
moduleCache.push(moduleName);
loadDependencies(moduleName).then(function success() {
try {
register(providers, moduleCache);
register(providers, moduleCache, params);
} catch(e) {
$log.error(e.message);
deferred.reject(e);
Expand Down Expand Up @@ -624,7 +629,7 @@
}
}

function invokeQueue(providers, queue) {
function invokeQueue(providers, queue, moduleName, reconfig) {
if(!queue) {
return;
}
Expand All @@ -638,7 +643,11 @@
} else {
throw new Error('unsupported provider ' + args[0]);
}
if(registerInvokeList(args[2][0])) {
var invoked = regConfigs.indexOf(moduleName);
if(registerInvokeList(args[2][0]) && (args[1] !== 'invoke' || (args[1] === 'invoke' && (!invoked || reconfig)))) {
if(!invoked) {
regConfigs.push(moduleName);
}
provider[args[1]].apply(provider, args[2]);
}
}
Expand All @@ -651,7 +660,7 @@
* @param registerModules
* @returns {*}
*/
function register(providers, registerModules) {
function register(providers, registerModules, params) {
if(registerModules) {
var k, moduleName, moduleFn, runBlocks = [];
for(k = registerModules.length - 1; k >= 0; k--) {
Expand All @@ -665,11 +674,11 @@
moduleFn = angular.module(moduleName);
if(regModules.indexOf(moduleName) === -1) { // new module
regModules.push(moduleName);
register(providers, moduleFn.requires);
register(providers, moduleFn.requires, params);
runBlocks = runBlocks.concat(moduleFn._runBlocks);
}
invokeQueue(providers, moduleFn._invokeQueue);
invokeQueue(providers, moduleFn._configBlocks);
invokeQueue(providers, moduleFn._invokeQueue, moduleName, params.reconfig);
invokeQueue(providers, moduleFn._configBlocks, moduleName, params.reconfig); // angular 1.3+
broadcast('ocLazyLoad.moduleLoaded', moduleName);
registerModules.pop();
}
Expand Down

0 comments on commit e2fec59

Please sign in to comment.