Hi All,
kangax said:
Richard Maher wrote:
So you decided to augment host objects after all.
Was there an option B? I couldn't find that hashCode() anywhere; what do you
use?
Anyway, thanks everyone for the very useful advice, and I think I've taken
the substantive points on board below.
Cheers Richard Maher
Take III :-
var listenerRegistry = function() {
var globalObj = this,
actionListeners = {},
targetId = 1,
listenerId = 1;
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 (!handler._fnId)
handler._fnId = listenerId++;
if (!element._evtId)
element._evtId = targetId++;
else
if (actionListeners[element._evtId + eventName +
handler._fnId])
return;
var normalizedHandler =
function() {
handler.call(
actionListeners[element._evtId +
eventName + handler._fnId].el,
globalObj.window.event);
};
actionListeners[element._evtId + eventName + handler._fnId]
=
{el : element, fn : normalizedHandler};
if (!element.attachEvent('on' + eventName,
normalizedHandler))
throw new Error("Unable to attach listener");
};
throw new Error("Unsupported Browser");
}();
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 (!element._evtId || !handler._fnId)
throw new Error("No such event registered on this
Object");
if (!actionListeners[element._evtId + eventName +
handler._fnId])
throw new Error("Unable to detach listener");
element.detachEvent('on' + eventName,
actionListeners[element._evtId +
eventName + handler._fnId].fn);
actionListeners[element._evtId + eventName +
handler._fnId].el = null;
actionListeners[element._evtId + eventName +
handler._fnId].fn = null;
actionListeners[element._evtId + eventName + handler._fnId]
= null;
};
throw new Error("Unsupported Browser");
}();
return {
checkIn : checkIn,
checkOut : checkOut
};
}();