I’m implementing the module pattern, and would like to know the best/preferred way to define and register event listeners/handlers. The following works, but maybe there is a better/simpler way…
var MODULE = function() {
// private
var _field1;
var _field2;
function localFunc(p) {
alert('localFunc');
}
// public
return {
// properties
prop1: _field1,
// events
myEvent1Handler: {},
myEvent1: function() {myEvent1Handler();},
myEvent2Handler: {},
myEvent2: function() {myEvent2Handler();},
addListener: function (event,func) {
if (event == "myEvent1")
myEvent1Handler = func;
if (event == "myEvent2")
myEvent2Handler = func;
},
// public methods
method1: function (p) {
alert('method1 says:' + p);
MODULE.myEvent1();
},
method2: function (p) {
alert('method2 doing stuff');
localFunc(p);
MODULE.myEvent2();
}
};
}();
// register for events
MODULE.addListener("myEvent1",function(){alert('fired1');});
MODULE.addListener("myEvent2",function(){alert('fired2');});
// use module (only event1 should fire!)
MODULE.method1("hello");
Try it out:
Seems like a lot of work to have myEventx, myEventHandlerx, and addListener?
Normally, I wouldn’t respond to an architectural question with a specific implementation (especially one dependent on a 3rd-party library like jQuery), but since my implementation promotes reusability — and we are talking patterns here — I will present a small jQuery plugin, $.eventable, which augments JavaScript objects with jQuery’s event methods.
This plugin will allow you to implement event handling capability on any object (or class instance) with one simple call.
If you include that snippet, you can implement your solution like this:
Your MODULE now has the following callable methods:
Where event is a space-delimited list of events, handler is your callback, and data is an optional value to pass to your callbacks.
You can refer to jQuery’s documentation for more details.