Skip to content

Commit

Permalink
fix: use init for bootstrapped apps & removed the need for loadedModules
Browse files Browse the repository at this point in the history
Fixes #84
Fixes #102
Fixes #109
  • Loading branch information
ocombe committed Dec 30, 2014
1 parent c745f55 commit 01936cd
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 135 deletions.
13 changes: 0 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ Load modules on demand (lazy load) in AngularJS
With $ocLazyLoad you can load angular modules, but if you want to load controllers / services / filters / ... without defining a new module it's entirely possible, just use the name an existing module (your app name for example).
There are multiple ways to use `$ocLazyLoad` to load your files, just choose the one that fits you the best.

###### Using requireJS:
If you decide to use ocLazyLoad with requireJS, you must declare the bootstraped module in the configuration of the provider. More info [here](#loaded-modules)

###### More examples / tutorials / articles
You can find three basic examples in the example folder. If you need more, check out these links:
- Lazy loading with requirejs, ocLazyLoad and ui-router: [using the templateProvider](http://plnkr.co/edit/OGvi01?p=preview) / [using the uiRouterDecorator](http://plnkr.co/edit/6CLDsz?p=preview) - by @gilbox
Expand Down Expand Up @@ -288,16 +285,6 @@ The options are:
});
```

- <a name="loaded-modules"></a>`loadedModules`: If you use angular.bootstrap(...) to launch your application, you need to define the main app module as a loaded module.
```js
angular.bootstrap(document.body, ['test']);
```
```js
$ocLazyLoadProvider.config({
loadedModules: ['test']
});
```

- `modules`: predefine the configuration of your modules for a later use
```js
$ocLazyLoadProvider.config({
Expand Down
117 changes: 57 additions & 60 deletions dist/ocLazyLoad.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
(function() {
'use strict';
var regModules = ['ng'],
initModules = [],
regInvokes = {},
regConfigs = [],
justLoaded = [],
Expand Down Expand Up @@ -625,17 +626,6 @@
templatesLoader = config.templatesLoader;
}

// for bootstrap apps, we need to define the main module name
if(angular.isDefined(config.loadedModules)) {
var addRegModule = function(loadedModule) {
if(regModules.indexOf(loadedModule) < 0) {
regModules.push(loadedModule);
angular.forEach(angular.module(loadedModule).requires, addRegModule);
}
};
angular.forEach(config.loadedModules, addRegModule);
}

// If we want to define modules configs
if(angular.isDefined(config.modules)) {
if(angular.isArray(config.modules)) {
Expand Down Expand Up @@ -870,63 +860,70 @@
* @param element
*/
function init(element) {
var elements = [element],
appElement,
moduleName,
names = ['ng:app', 'ng-app', 'x-ng-app', 'data-ng-app'],
NG_APP_CLASS_REGEXP = /\sng[:\-]app(:\s*([\w\d_]+);?)?\s/;

function append(elm) {
return (elm && elements.push(elm));
}
if(initModules.length === 0) {
var elements = [element],
names = ['ng:app', 'ng-app', 'x-ng-app', 'data-ng-app'],
NG_APP_CLASS_REGEXP = /\sng[:\-]app(:\s*([\w\d_]+);?)?\s/,
append = function append(elm) {
return (elm && elements.push(elm));
};

angular.forEach(names, function(name) {
names[name] = true;
append(document.getElementById(name));
name = name.replace(':', '\\:');
if(element[0].querySelectorAll) {
angular.forEach(element[0].querySelectorAll('.' + name), append);
angular.forEach(element[0].querySelectorAll('.' + name + '\\:'), append);
angular.forEach(element[0].querySelectorAll('[' + name + ']'), append);
}
});
angular.forEach(names, function(name) {
names[name] = true;
append(document.getElementById(name));
name = name.replace(':', '\\:');
if(element[0].querySelectorAll) {
angular.forEach(element[0].querySelectorAll('.' + name), append);
angular.forEach(element[0].querySelectorAll('.' + name + '\\:'), append);
angular.forEach(element[0].querySelectorAll('[' + name + ']'), append);
}
});

//TODO: search the script tags for angular.bootstrap
angular.forEach(elements, function(elm) {
if(!appElement) {
var className = ' ' + element.className + ' ';
var match = NG_APP_CLASS_REGEXP.exec(className);
if(match) {
appElement = elm;
moduleName = (match[2] || '').replace(/\s+/g, ',');
} else {
angular.forEach(elm.attributes, function(attr) {
if(!appElement && names[attr.name]) {
appElement = elm;
moduleName = attr.value;
}
});
angular.forEach(elements, function(elm) {
if(initModules.length === 0) {
var className = ' ' + element.className + ' ';
var match = NG_APP_CLASS_REGEXP.exec(className);
if(match) {
initModules.push((match[2] || '').replace(/\s+/g, ','));
} else {
angular.forEach(elm.attributes, function(attr) {
if(initModules.length === 0 && names[attr.name]) {
initModules.push(attr.value);
}
});
}
}
}
});
});
}
if(initModules.length === 0) {
throw 'No module found during bootstrap, unable to init ocLazyLoad';
}

if(appElement) {
(function addReg(moduleName) {
if(regModules.indexOf(moduleName) === -1) {
// register existing modules
regModules.push(moduleName);
var mainModule = angular.module(moduleName);
var addReg = function addReg(moduleName) {
if(regModules.indexOf(moduleName) === -1) {
// register existing modules
regModules.push(moduleName);
var mainModule = angular.module(moduleName);

// register existing components (directives, services, ...)
invokeQueue(null, mainModule._invokeQueue, moduleName);
invokeQueue(null, mainModule._configBlocks, moduleName); // angular 1.3+
// register existing components (directives, services, ...)
invokeQueue(null, mainModule._invokeQueue, moduleName);
invokeQueue(null, mainModule._configBlocks, moduleName); // angular 1.3+

angular.forEach(mainModule.requires, addReg);
}
})(moduleName);
}
angular.forEach(mainModule.requires, addReg);
}
};

angular.forEach(initModules, function(moduleName) {
addReg(moduleName);
});
}

var bootstrap = angular.bootstrap;
angular.bootstrap = function(element, modules, config) {
initModules = modules.slice(); // make a clean copy
return bootstrap(element, modules, config);
};

// Array.indexOf polyfill for IE8
if(!Array.prototype.indexOf) {
Array.prototype.indexOf = function(searchElement, fromIndex) {
Expand Down

0 comments on commit 01936cd

Please sign in to comment.