diff --git a/public/js/icinga.js b/public/js/icinga.js index ee8a7c11a..e8b8bccc8 100644 --- a/public/js/icinga.js +++ b/public/js/icinga.js @@ -163,6 +163,37 @@ return 'undefined' !== typeof this.modules[name]; }, + /** + * Ensure we have loaded the javascript code for a module + * + * @param {string} moduleName + */ + ensureModule: function(moduleName) { + if (this.hasModule(moduleName) && ! this.isLoadedModule(moduleName)) { + this.loadModule(moduleName); + } + }, + + /** + * If a container contains sub-containers for other modules, + * make sure the javascript code for each module is loaded. + * + * Containers are identified by "data-icinga-module" which + * holds the module name. + * + * @param container + */ + ensureSubModules: function (container) { + var icinga = this; + + $(container).find('[data-icinga-module]').each(function () { + var moduleName = $(this).data('icingaModule'); + if (moduleName) { + icinga.ensureModule(moduleName); + } + }); + }, + /** * Get a module by name * diff --git a/public/js/icinga/events.js b/public/js/icinga/events.js index a38da25ef..befab0794 100644 --- a/public/js/icinga/events.js +++ b/public/js/icinga/events.js @@ -35,16 +35,8 @@ behavior.bind($(document)); }); - var _this = this; - $('.container').each(function () { - // Initialize module javascript (Applies only to module.js code) - var moduleName = $(this).data('icingaModule'); - if (moduleName) { - if (_this.icinga.hasModule(moduleName) && ! _this.icinga.isLoadedModule(moduleName)) { - _this.icinga.loadModule(moduleName); - } - } - }); + // Initialize module javascript (Applies only to module.js code) + this.icinga.ensureSubModules(document); // We catch resize events $(window).on('resize', { self: this.icinga.ui }, this.icinga.ui.onWindowResize); diff --git a/public/js/icinga/loader.js b/public/js/icinga/loader.js index 85adcdf4c..1de675a09 100644 --- a/public/js/icinga/loader.js +++ b/public/js/icinga/loader.js @@ -734,9 +734,7 @@ }); if (moduleName) { // Lazy load module javascript (Applies only to module.js code) - if (_this.icinga.hasModule(moduleName) && ! _this.icinga.isLoadedModule(moduleName)) { - _this.icinga.loadModule(moduleName); - } + _this.icinga.ensureModule(moduleName); req.$target.data('icingaModule', moduleName); classes.push('icinga-module'); @@ -1025,13 +1023,10 @@ } } - req.$target.find('.container').each(function () { - // Lazy load module javascript (Applies only to module.js code) - var moduleName = $(this).data('icingaModule'); - if (_this.icinga.hasModule(moduleName) && ! _this.icinga.isLoadedModule(moduleName)) { - _this.icinga.loadModule(moduleName); - } + // Lazy load module javascript (Applies only to module.js code) + this.icinga.ensureSubModules(req.$target); + req.$target.find('.container').each(function () { $(this).trigger('rendered', [req.autorefresh, req.scripted, req.autosubmit]); }); req.$target.trigger('rendered', [req.autorefresh, req.scripted, req.autosubmit]);