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
This commit is contained in:
Thomas Gelf 2014-03-31 08:38:00 +00:00
parent f67b83fb75
commit d0fd25d4ef
2 changed files with 22 additions and 20 deletions

View File

@ -435,6 +435,7 @@
if (active) { if (active) {
$('[href="' + active + '"]', req.$target).addClass('active'); $('[href="' + active + '"]', req.$target).addClass('active');
} }
req.$target.trigger('rendered');
}, },
/** /**

View File

@ -10,10 +10,11 @@
// The Icinga instance // The Icinga instance
this.icinga = icinga; this.icinga = icinga;
// Event handlers registered by this module // Applied event handlers
this.handlers = []; this.handlers = [];
this.registeredHandlers = {}; // Event handlers registered by this module
this.registeredHandlers = [];
// The module name // The module name
this.name = name; this.name = name;
@ -44,7 +45,7 @@
// The constructor of the modules prototype must be prepared to get an // The constructor of the modules prototype must be prepared to get an
// instance of Icinga.Module // instance of Icinga.Module
this.object = new this.prototyp(this); this.object = new this.prototyp(this);
this.applyRegisteredEventHandlers(); this.applyHandlers();
} catch(e) { } catch(e) {
this.icinga.logger.error( this.icinga.logger.error(
'Failed to load module ' + this.name + ': ', 'Failed to load module ' + this.name + ': ',
@ -63,29 +64,29 @@
}, },
/** /**
* Globally register this modules event handlers * Register this modules event handlers
*/ */
registerEventHandlers: function (handlers) { on: function (event, filter, handler) {
this.registeredHandlers = handlers; if (typeof handler === 'undefined') {
return this; 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; var self = this;
$.each(this.registeredHandlers, function (filter, events) { $.each(this.registeredHandlers, function (key, on) {
$.each(events, function (event, handler) {
// TODO: if (event[1] === 'each') {
// $(event[0], $(el)).each(event[2]);
self.bindEventHandler( self.bindEventHandler(
event, on.event,
'.module-' + self.name + ' ' + filter, on.filter,
handler on.handler
); );
}); });
});
self = null; self = null;
return this; return this;