On Tue, Mar 29, 2011 at 12:17 PM, Mark McDonnell <[email protected]> wrote:
> Thanks Michael, I'm not tied to that particular W3C style model so I'm happy
> to give it a try what you have suggested. It does appear to be a much
> simpler process but has the minor downside of a different API for users to
> learn (not a big deal at all, but when everyone knows the standard way of
> doing things, changing the API to make the development of the library easier
> just feels like a easy way out?)
> But like I say, I'm not tied to that model in any way and am happy to get
> stuff working at this stage and then re-visit later to see what can be done
> :-)
> Ps, understood about the large image and the pasting of the code. But I did
> see Rey Bango mention previously that there were some 'rules' to posting
> that meant we couldn't just link off to a Gist or JsFiddle and so we had to
> paste in code for others to be able to split up with their comments? But the
> screen shot image was probably overkill :-)
> Ta!
> Mark
>
> --
> Mark McDonnell
>
> On Monday, 28 March 2011 at 20:56, Michael Geary wrote:
>
> On Mon, Mar 28, 2011 at 8:53 AM, Mark McDonnell <[email protected]> wrote:
>
> I've uploaded a copy of my script here: https://gist.github.com/890700 (copy
> of the script is also listed below)
>
> Just the link to the gist is all that's needed - the code you posted to the
> list got mangled anyway. And let's skip the 432KB screenshots too. :-) If a
> screenshot helps illustrate your problem, upload it somewhere and post a
> link.
> So back to your code... Is it a requirement that your library API follow the
> DOM event listener model where you pass a reference to the original callback
> function when you detach an event listener? That was a really poor design
> decision in the DOM APIs, and unless you *must* follow that model, you can
> do better.
> Simply have your add() function create and return an object. Save a
> reference to the original event listener in the object, and give the object
> a .remove() method. That method then has a reference to the original
> callback which it can use to call the appropriate DOM remove method.
> In skeletal form:
> // Using the API
> var listener = add( element, 'click', function() {
> alert( 'click' );
> });
> // ...
> listener.remove();
> // Implementing the API
> function add( element, event, callback ) {
> var listener = {
> callback: callback,
> remove: function() {
> // pass listener.callback to the DOM remove function here
> }
> };
> // add DOM event listener here
> }
> This is much simpler and more efficient than having to keep a list of
> listener functions so you can find them later. And it makes your API easier
> to use too: now you can freely use anonymous functions for your event
> listeners and still be able to remove them later.
> -Mike
>
> --
> To view archived discussions from the original JSMentors Mailman list:
> http://www.mail-archive.com/[email protected]/
>
> To search via a non-Google archive, visit here:
> http://www.mail-archive.com/[email protected]/
>
> To unsubscribe from this group, send email to
> [email protected]
>
> --
> To view archived discussions from the original JSMentors Mailman list:
> http://www.mail-archive.com/[email protected]/
>
> To search via a non-Google archive, visit here:
> http://www.mail-archive.com/[email protected]/
>
> To unsubscribe from this group, send email to
> [email protected]
>
Just to underline that the DOM2 Event specifications already let us do this:
var listener = {
handleEvent: function(event) {
element.removeEventListener(event.type, listener, false);
callback(event); // user specified processing
}
};
element.addEventListener('click', listener, false);
this is a mini example of a self removing listener which will work
even if ES5 "use strict;" mode is enforced since neither the special
"this" keyword nor the deprecated "arguments.callee" are used for
removal.
In a few words, the 2nd argument to "[add|remove]EventListener()" can
also be a standard object, not just a callable one (a function).
Also see this Ajaxian article about it:
http://ajaxian.com/archives/an-alternative-way-to-addeventlistener
You have more alternatives now but you should still fix IE < 9 if you care ;-)
--
Diego
--
To view archived discussions from the original JSMentors Mailman list:
http://www.mail-archive.com/[email protected]/
To search via a non-Google archive, visit here:
http://www.mail-archive.com/[email protected]/
To unsubscribe from this group, send email to
[email protected]