Skip to content

Commit

Permalink
feat: new event ocLazyLoad.moduleReloaded
Browse files Browse the repository at this point in the history
I added this event to make the distinction between a newly loaded module and a reloaded module
  • Loading branch information
ocombe committed Sep 10, 2014
1 parent 6752bb9 commit 5010d14
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 33 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ The options are:
});
```

- `events`: $ocLazyLoad can broadcast an event when you load a module, a component and a file (js/css/template). It is disabled by default, set events to true to activate it. The events are `ocLazyLoad.moduleLoaded`, `ocLazyLoad.componentLoaded`, `ocLazyLoad.fileLoaded`.
- `events`: $ocLazyLoad can broadcast an event when you load a module, a component and a file (js/css/template). It is disabled by default, set events to true to activate it. The events are `ocLazyLoad.moduleLoaded`, `ocLazyLoad.moduleReloaded`, `ocLazyLoad.componentLoaded`, `ocLazyLoad.fileLoaded`.
```js
$ocLazyLoadProvider.config({
events: true
Expand Down
22 changes: 14 additions & 8 deletions dist/ocLazyLoad.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
var regModules = ['ng'],
regInvokes = [],
regConfigs = [],
justLoaded = [],
ocLazyLoad = angular.module('oc.lazyLoad', ['ng']),
broadcast = angular.noop;

Expand Down Expand Up @@ -464,6 +465,7 @@
moduleCache.push(moduleName);
loadDependencies(moduleName).then(function success() {
try {
justLoaded = [];
register(providers, moduleCache, params);
} catch(e) {
$log.error(e.message);
Expand Down Expand Up @@ -711,19 +713,21 @@
if(typeof moduleName !== 'string') {
moduleName = getModuleName(moduleName);
}
if(!moduleName) {
if(!moduleName || justLoaded.indexOf(moduleName) !== -1) {
continue;
}
var newModule = regModules.indexOf(moduleName) === -1;
moduleFn = angular.module(moduleName);
if(regModules.indexOf(moduleName) === -1) { // new module
if(newModule) { // new module
regModules.push(moduleName);
register(providers, moduleFn.requires, params);
runBlocks = runBlocks.concat(moduleFn._runBlocks);
}
invokeQueue(providers, moduleFn._invokeQueue, moduleName, params.reconfig);
invokeQueue(providers, moduleFn._configBlocks, moduleName, params.reconfig); // angular 1.3+
broadcast('ocLazyLoad.moduleLoaded', moduleName);
broadcast(newModule ? 'ocLazyLoad.moduleLoaded' : 'ocLazyLoad.moduleReloaded', moduleName);
registerModules.pop();
justLoaded.push(moduleName);
}
var instanceInjector = providers.getInstanceInjector();
angular.forEach(runBlocks, function(fn) {
Expand All @@ -739,17 +743,19 @@
*/
function registerInvokeList(invokeList) {
var newInvoke = false;
var onInvoke = function(invokeName) {
newInvoke = true;
regInvokes.push(invokeName);
broadcast('ocLazyLoad.componentLoaded', invokeName);
}
if(angular.isString(invokeList)) {
if(regInvokes.indexOf(invokeList) === -1) {
newInvoke = true;
regInvokes.push(invokeList);
broadcast('ocLazyLoad.componentLoaded', invokeList);
onInvoke(invokeList);
}
} else if(angular.isObject(invokeList)) {
angular.forEach(invokeList, function(invoke) {
if(angular.isString(invoke) && regInvokes.indexOf(invoke) === -1) {
newInvoke = true;
regInvokes.push(invoke);
onInvoke(invoke);
}
});
} else {
Expand Down
Loading

0 comments on commit 5010d14

Please sign in to comment.