Skip to content

Commit 364c9e9

Browse files
committed
feat: new function: isLoaded to check if a module has been loaded
This function lets you check if a module (or a list of modules) has been loaded into Angular or not. Closes #79
1 parent 07e3689 commit 364c9e9

6 files changed

Lines changed: 249 additions & 37 deletions

File tree

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,18 @@ $stateProvider.state('index', {
366366
```
367367

368368

369+
##Other functions
370+
`$ocLazyLoad` provides a few other functions that might help you in a few specific cases:
371+
372+
- `setModuleConfig(moduleConfig)`: Let you define a module config object
373+
374+
- `getModuleConfig(moduleName)`: Let you get a module config object
375+
376+
- `getModules()`: Returns the list of loaded modules
377+
378+
- `isLoaded(modulesNames)`: Let you check if a module (or a list of modules) has been loaded into Angular or not
379+
380+
369381
##Contribute
370382
If you want to get started and the docs are not enough, see the examples in the 'example' folder !
371383

dist/ocLazyLoad.js

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -315,27 +315,77 @@
315315
}
316316

317317
return {
318-
getModuleConfig: function(name) {
319-
if(!modules[name]) {
318+
/**
319+
* Let you get a module config object
320+
* @param moduleName String the name of the module
321+
* @returns {*}
322+
*/
323+
getModuleConfig: function(moduleName) {
324+
if(!angular.isString(moduleName)) {
325+
throw new Error('You need to give the name of the module to get');
326+
}
327+
if(!modules[moduleName]) {
320328
return null;
321329
}
322-
return modules[name];
330+
return modules[moduleName];
323331
},
324332

325-
setModuleConfig: function(module) {
326-
modules[module.name] = module;
327-
return module;
333+
/**
334+
* Let you define a module config object
335+
* @param moduleConfig Object the module config object
336+
* @returns {*}
337+
*/
338+
setModuleConfig: function(moduleConfig) {
339+
if(!angular.isObject(moduleConfig)) {
340+
throw new Error('You need to give the module config object to set');
341+
}
342+
modules[moduleConfig.name] = moduleConfig;
343+
return moduleConfig;
328344
},
329345

346+
/**
347+
* Returns the list of loaded modules
348+
* @returns {string[]}
349+
*/
330350
getModules: function() {
331351
return regModules;
332352
},
333353

334-
// deprecated
335-
loadTemplateFile: function(paths, params) {
336-
return filesLoader({files: paths}, params);
354+
/**
355+
* Let you check if a module has been loaded into Angular or not
356+
* @param modulesNames String/Object a module name, or a list of module names
357+
* @returns {boolean}
358+
*/
359+
isLoaded: function(modulesNames) {
360+
var moduleLoaded = function(module) {
361+
var isLoaded = regModules.indexOf(module) > -1;
362+
if(!isLoaded) {
363+
isLoaded = !!moduleExists(module);
364+
}
365+
return isLoaded;
366+
}
367+
if(angular.isString(modulesNames)) {
368+
modulesNames = [modulesNames];
369+
}
370+
if(angular.isArray(modulesNames)) {
371+
var i, len;
372+
for(i = 0, len = modulesNames.length; i < len; i++) {
373+
if(!moduleLoaded(modulesNames[i])) {
374+
return false;
375+
}
376+
}
377+
return true;
378+
} else {
379+
throw new Error('You need to define the module(s) name(s)');
380+
}
337381
},
338382

383+
/**
384+
* Load a module or a list of modules into Angular
385+
* @param module Mixed the name of a predefined module config object, or a module config object, or an array of either
386+
* @param params Object optional parameters
387+
* @returns promise
388+
*/
339389
load: function(module, params) {
340390
var self = this,
341391
config = null,

dist/ocLazyLoad.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/example1/js/ocLazyLoad.js

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -315,27 +315,77 @@
315315
}
316316

317317
return {
318-
getModuleConfig: function(name) {
319-
if(!modules[name]) {
318+
/**
319+
* Let you get a module config object
320+
* @param moduleName String the name of the module
321+
* @returns {*}
322+
*/
323+
getModuleConfig: function(moduleName) {
324+
if(!angular.isString(moduleName)) {
325+
throw new Error('You need to give the name of the module to get');
326+
}
327+
if(!modules[moduleName]) {
320328
return null;
321329
}
322-
return modules[name];
330+
return modules[moduleName];
323331
},
324332

325-
setModuleConfig: function(module) {
326-
modules[module.name] = module;
327-
return module;
333+
/**
334+
* Let you define a module config object
335+
* @param moduleConfig Object the module config object
336+
* @returns {*}
337+
*/
338+
setModuleConfig: function(moduleConfig) {
339+
if(!angular.isObject(moduleConfig)) {
340+
throw new Error('You need to give the module config object to set');
341+
}
342+
modules[moduleConfig.name] = moduleConfig;
343+
return moduleConfig;
328344
},
329345

346+
/**
347+
* Returns the list of loaded modules
348+
* @returns {string[]}
349+
*/
330350
getModules: function() {
331351
return regModules;
332352
},
333353

334-
// deprecated
335-
loadTemplateFile: function(paths, params) {
336-
return filesLoader({files: paths}, params);
354+
/**
355+
* Let you check if a module has been loaded into Angular or not
356+
* @param modulesNames String/Object a module name, or a list of module names
357+
* @returns {boolean}
358+
*/
359+
isLoaded: function(modulesNames) {
360+
var moduleLoaded = function(module) {
361+
var isLoaded = regModules.indexOf(module) > -1;
362+
if(!isLoaded) {
363+
isLoaded = !!moduleExists(module);
364+
}
365+
return isLoaded;
366+
}
367+
if(angular.isString(modulesNames)) {
368+
modulesNames = [modulesNames];
369+
}
370+
if(angular.isArray(modulesNames)) {
371+
var i, len;
372+
for(i = 0, len = modulesNames.length; i < len; i++) {
373+
if(!moduleLoaded(modulesNames[i])) {
374+
return false;
375+
}
376+
}
377+
return true;
378+
} else {
379+
throw new Error('You need to define the module(s) name(s)');
380+
}
337381
},
338382

383+
/**
384+
* Load a module or a list of modules into Angular
385+
* @param module Mixed the name of a predefined module config object, or a module config object, or an array of either
386+
* @param params Object optional parameters
387+
* @returns promise
388+
*/
339389
load: function(module, params) {
340390
var self = this,
341391
config = null,

examples/example2/js/ocLazyLoad.js

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -315,27 +315,77 @@
315315
}
316316

317317
return {
318-
getModuleConfig: function(name) {
319-
if(!modules[name]) {
318+
/**
319+
* Let you get a module config object
320+
* @param moduleName String the name of the module
321+
* @returns {*}
322+
*/
323+
getModuleConfig: function(moduleName) {
324+
if(!angular.isString(moduleName)) {
325+
throw new Error('You need to give the name of the module to get');
326+
}
327+
if(!modules[moduleName]) {
320328
return null;
321329
}
322-
return modules[name];
330+
return modules[moduleName];
323331
},
324332

325-
setModuleConfig: function(module) {
326-
modules[module.name] = module;
327-
return module;
333+
/**
334+
* Let you define a module config object
335+
* @param moduleConfig Object the module config object
336+
* @returns {*}
337+
*/
338+
setModuleConfig: function(moduleConfig) {
339+
if(!angular.isObject(moduleConfig)) {
340+
throw new Error('You need to give the module config object to set');
341+
}
342+
modules[moduleConfig.name] = moduleConfig;
343+
return moduleConfig;
328344
},
329345

346+
/**
347+
* Returns the list of loaded modules
348+
* @returns {string[]}
349+
*/
330350
getModules: function() {
331351
return regModules;
332352
},
333353

334-
// deprecated
335-
loadTemplateFile: function(paths, params) {
336-
return filesLoader({files: paths}, params);
354+
/**
355+
* Let you check if a module has been loaded into Angular or not
356+
* @param modulesNames String/Object a module name, or a list of module names
357+
* @returns {boolean}
358+
*/
359+
isLoaded: function(modulesNames) {
360+
var moduleLoaded = function(module) {
361+
var isLoaded = regModules.indexOf(module) > -1;
362+
if(!isLoaded) {
363+
isLoaded = !!moduleExists(module);
364+
}
365+
return isLoaded;
366+
}
367+
if(angular.isString(modulesNames)) {
368+
modulesNames = [modulesNames];
369+
}
370+
if(angular.isArray(modulesNames)) {
371+
var i, len;
372+
for(i = 0, len = modulesNames.length; i < len; i++) {
373+
if(!moduleLoaded(modulesNames[i])) {
374+
return false;
375+
}
376+
}
377+
return true;
378+
} else {
379+
throw new Error('You need to define the module(s) name(s)');
380+
}
337381
},
338382

383+
/**
384+
* Load a module or a list of modules into Angular
385+
* @param module Mixed the name of a predefined module config object, or a module config object, or an array of either
386+
* @param params Object optional parameters
387+
* @returns promise
388+
*/
339389
load: function(module, params) {
340390
var self = this,
341391
config = null,

src/ocLazyLoad.js

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -308,27 +308,77 @@
308308
}
309309

310310
return {
311-
getModuleConfig: function(name) {
312-
if(!modules[name]) {
311+
/**
312+
* Let you get a module config object
313+
* @param moduleName String the name of the module
314+
* @returns {*}
315+
*/
316+
getModuleConfig: function(moduleName) {
317+
if(!angular.isString(moduleName)) {
318+
throw new Error('You need to give the name of the module to get');
319+
}
320+
if(!modules[moduleName]) {
313321
return null;
314322
}
315-
return modules[name];
323+
return modules[moduleName];
316324
},
317325

318-
setModuleConfig: function(module) {
319-
modules[module.name] = module;
320-
return module;
326+
/**
327+
* Let you define a module config object
328+
* @param moduleConfig Object the module config object
329+
* @returns {*}
330+
*/
331+
setModuleConfig: function(moduleConfig) {
332+
if(!angular.isObject(moduleConfig)) {
333+
throw new Error('You need to give the module config object to set');
334+
}
335+
modules[moduleConfig.name] = moduleConfig;
336+
return moduleConfig;
321337
},
322338

339+
/**
340+
* Returns the list of loaded modules
341+
* @returns {string[]}
342+
*/
323343
getModules: function() {
324344
return regModules;
325345
},
326346

327-
// deprecated
328-
loadTemplateFile: function(paths, params) {
329-
return filesLoader({files: paths}, params);
347+
/**
348+
* Let you check if a module has been loaded into Angular or not
349+
* @param modulesNames String/Object a module name, or a list of module names
350+
* @returns {boolean}
351+
*/
352+
isLoaded: function(modulesNames) {
353+
var moduleLoaded = function(module) {
354+
var isLoaded = regModules.indexOf(module) > -1;
355+
if(!isLoaded) {
356+
isLoaded = !!moduleExists(module);
357+
}
358+
return isLoaded;
359+
}
360+
if(angular.isString(modulesNames)) {
361+
modulesNames = [modulesNames];
362+
}
363+
if(angular.isArray(modulesNames)) {
364+
var i, len;
365+
for(i = 0, len = modulesNames.length; i < len; i++) {
366+
if(!moduleLoaded(modulesNames[i])) {
367+
return false;
368+
}
369+
}
370+
return true;
371+
} else {
372+
throw new Error('You need to define the module(s) name(s)');
373+
}
330374
},
331375

376+
/**
377+
* Load a module or a list of modules into Angular
378+
* @param module Mixed the name of a predefined module config object, or a module config object, or an array of either
379+
* @param params Object optional parameters
380+
* @returns promise
381+
*/
332382
load: function(module, params) {
333383
var self = this,
334384
config = null,

0 commit comments

Comments
 (0)