From d0fd25d4ef24e64c115c6ee6a1c3fd59657efde4 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Mon, 31 Mar 2014 08:38:00 +0000 Subject: [PATCH] Extend module event handler capabilities, allow for easier use * introduce 'rendered' event for modules * use this.module.on() instead of registerEventHandlers() * no CSS filter creates event handlers on module containers --- public/js/icinga/loader.js | 1 + public/js/icinga/module.js | 41 +++++++++++++++++++------------------- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/public/js/icinga/loader.js b/public/js/icinga/loader.js index 0ed0e17c6..9b75a6fc1 100644 --- a/public/js/icinga/loader.js +++ b/public/js/icinga/loader.js @@ -435,6 +435,7 @@ if (active) { $('[href="' + active + '"]', req.$target).addClass('active'); } + req.$target.trigger('rendered'); }, /** diff --git a/public/js/icinga/module.js b/public/js/icinga/module.js index f43ce1a6f..f43dfbd42 100644 --- a/public/js/icinga/module.js +++ b/public/js/icinga/module.js @@ -10,10 +10,11 @@ // The Icinga instance this.icinga = icinga; - // Event handlers registered by this module + // Applied event handlers this.handlers = []; - this.registeredHandlers = {}; + // Event handlers registered by this module + this.registeredHandlers = []; // The module name this.name = name; @@ -44,7 +45,7 @@ // The constructor of the modules prototype must be prepared to get an // instance of Icinga.Module this.object = new this.prototyp(this); - this.applyRegisteredEventHandlers(); + this.applyHandlers(); } catch(e) { this.icinga.logger.error( 'Failed to load module ' + this.name + ': ', @@ -63,28 +64,28 @@ }, /** - * Globally register this modules event handlers + * Register this modules event handlers */ - registerEventHandlers: function (handlers) { - this.registeredHandlers = handlers; - return this; + on: function (event, filter, handler) { + if (typeof handler === 'undefined') { + handler = filter; + filter = '.module-' + this.name; + } else { + filter = '.module-' + this.name + ' ' + filter; + } + this.registeredHandlers.push({event: event, filter: filter, handler: handler}); + }, - applyRegisteredEventHandlers: function () { - + applyHandlers: function () { var self = this; - $.each(this.registeredHandlers, function (filter, events) { - - $.each(events, function (event, handler) { - // TODO: if (event[1] === 'each') { - // $(event[0], $(el)).each(event[2]); - self.bindEventHandler( - event, - '.module-' + self.name + ' ' + filter, - handler - ); - }); + $.each(this.registeredHandlers, function (key, on) { + self.bindEventHandler( + on.event, + on.filter, + on.handler + ); }); self = null;