Trying to standardise event object without breaking removeEventListener

I

Integralist

Hi,

As the subject suggests I'm trying to work out a way I can get this (very basic) library I'm working on to be able standardise the event object for users so they dont have to.

I've set-up a jsFiddle here: http://jsfiddle.net/3QVhY/

If you click on either the red/blue border and check your console you'll see some logged information, and the if you click on the 'remove listener' button and do the same again then nothing extra will happen - which is fine, that's exactly what it's supposed to do.

The problem I have is that if I want to standardise the event object, the only way I know how is to wrap the users specified handler function in an anonymous function within the addEventListener call (which passes through theevent object) and I then call the users handler but pass through the standardised version of the event.

But this breaks the removeEventListener because anonymous functions aren't seen as the same.

So me doing this...

eventType = eventType.toLowerCase();
element.addEventListener(eventType, function(e) {
handler(__mylib.events.standardize(e));
}, false);

....doesn't work.

Does anyone know how the majority of JavaScript libraries achieve this typeof feature whereby the event object is standardised?

Also, I have checked a couple of the popular js libraries and found that none of them were able to provide an api to work around when/if a user uses an anonymous function while setting up an event listener. But these libraries were capable of returning to the handler function a standardised event object.

It maybe that I have to set-up my library differently to achieve this type of functionality, although I'm not sure how I'd go about doing this so I'd need advice on this as well.

Regards,
Mark
 
V

VK

So me doing this...

eventType = eventType.toLowerCase();
element.addEventListener(eventType, function(e) {
    handler(__mylib.events.standardize(e));

}, false);

...doesn't work.

Does anyone know how the majority of JavaScript libraries achieve this type of feature whereby the event object is standardised?

What I'm missing in your approach is the necessity to bother with
wrappers at all.
If each event callback function will have to work with a refactored
event object and not with the original one: that means that each
callback function has to be written in some uniform way.
For the starter there cannot be that one callback will relay on the
first argument and another one on global "event" object (MS). So they
all relay on the first argument and its certain properties so skipping
on "e - event" checks and the like. Otherwise there is no purpose to
bother with a standardized event object at all.
With all this in place each callback should simply start with
var evt = __mylib.events.standardize(e);
and move from that point onward. So it is not a programming question
but documentation question: you should clearly document the necessary
component and way of property access for event listeners in your
library. Also explain to users: minus - extra rules to remember, plus
- skip on tedious initial checks and normalizations.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top