R
Richard Maher
Hi,
I'd like to thank David Mark for recently sending me a copy of his MyLib.js
library and for responding to some of my questions here in c.l.js. (Let me
also take the opportunity to thank all of you who have responded with
assistance to my posts!).
As it happens, my requirements are more pedestrian/light-weight when it
comes to the event-listener requirements than the full-throttle
all-encompassing functionality that David's library provides so I hope
someone/anyone can assist on a dumbed-down event listener strategy.
Attached below with listenerRegistry.checkIn and chekOut is (what I think
is) all I need. The fly in the oinment here would appear to be
circular-references and IE's memory leakage issues. Now, I'm more than happy
to stick an ONUNLOAD handler in there and loop through the registry (like
David's and many other strategys have done) but is it really needed in this
case? Will there be leakage and do I have to cater for it?
Also if there's much else that's bollocks with the example please let me
know.
Cheers Richard Maher
var listenerRegistry = function() {
var globalVar = function(){return this;}();
var actionListeners = {};
var checkIn = function(){
if (window.addEventListener) {
return function(element, eventName, handler) {
element.addEventListener(eventName, handler, false);
};
}
if (window.attachEvent) {
return function(element, eventName, handler) {
if (actionListeners[element + eventName + handler])
return;
var normalizedHandler =
function() {
handler.call(
actionListeners[element + eventName +
handler].el,
globalVar.event);
};
actionListeners[element + eventName + handler] =
{el : element, fn : normalizedHandler};
if (!element.attachEvent('on' + eventName,
normalizedHandler))
throw new Error("Unable to attach listener");
};
}
}();
var checkOut = function(){
if (window.removeEventListener) {
return function(element, eventName, handler) {
element.removeEventListener(eventName, handler, false);
};
}
if (window.detachEvent) {
return function(element, eventName, handler) {
if (!actionListeners[element + eventName + handler])
throw new Error("Unable to detach listener");
element.detachEvent('on' + eventName,
actionListeners[element + eventName +
handler].fn);
actionListeners[element + eventName + handler].el = null;
actionListeners[element + eventName + handler].fn = null;
actionListeners[element + eventName + handler] = null;
};
}
}();
return {
checkIn : checkIn,
checkOut : checkOut
};
}();
I'd like to thank David Mark for recently sending me a copy of his MyLib.js
library and for responding to some of my questions here in c.l.js. (Let me
also take the opportunity to thank all of you who have responded with
assistance to my posts!).
As it happens, my requirements are more pedestrian/light-weight when it
comes to the event-listener requirements than the full-throttle
all-encompassing functionality that David's library provides so I hope
someone/anyone can assist on a dumbed-down event listener strategy.
Attached below with listenerRegistry.checkIn and chekOut is (what I think
is) all I need. The fly in the oinment here would appear to be
circular-references and IE's memory leakage issues. Now, I'm more than happy
to stick an ONUNLOAD handler in there and loop through the registry (like
David's and many other strategys have done) but is it really needed in this
case? Will there be leakage and do I have to cater for it?
Also if there's much else that's bollocks with the example please let me
know.
Cheers Richard Maher
var listenerRegistry = function() {
var globalVar = function(){return this;}();
var actionListeners = {};
var checkIn = function(){
if (window.addEventListener) {
return function(element, eventName, handler) {
element.addEventListener(eventName, handler, false);
};
}
if (window.attachEvent) {
return function(element, eventName, handler) {
if (actionListeners[element + eventName + handler])
return;
var normalizedHandler =
function() {
handler.call(
actionListeners[element + eventName +
handler].el,
globalVar.event);
};
actionListeners[element + eventName + handler] =
{el : element, fn : normalizedHandler};
if (!element.attachEvent('on' + eventName,
normalizedHandler))
throw new Error("Unable to attach listener");
};
}
}();
var checkOut = function(){
if (window.removeEventListener) {
return function(element, eventName, handler) {
element.removeEventListener(eventName, handler, false);
};
}
if (window.detachEvent) {
return function(element, eventName, handler) {
if (!actionListeners[element + eventName + handler])
throw new Error("Unable to detach listener");
element.detachEvent('on' + eventName,
actionListeners[element + eventName +
handler].fn);
actionListeners[element + eventName + handler].el = null;
actionListeners[element + eventName + handler].fn = null;
actionListeners[element + eventName + handler] = null;
};
}
}();
return {
checkIn : checkIn,
checkOut : checkOut
};
}();