I would like to share a modification to bindAsEventListener() that might be useful to some of you. It makes it possible to do function show(event, number) { alert(Event.element(event).id + " = " + number); } Event.observe("el1", "click", show.bindAsEventListener(this, 5); Event.observe("el2", "click", show.bindAsEventListener(this, 10); That will alert "el1 = 5" when el1 is clicked and "el1 = 10" when el2 is clicked. The changed function: Function.prototype.bindAsEventListener = function(object) { var __method = this; var argumente = $A(arguments); argumente.shift(); // remove "object" from the list => just the additional arguments remain return function(event) { var ar = argumente.slice(0); // copy values ar.unshift(event || window.event); return __method.apply(object, ar); } } Bye, Martin
On 2/27/06, Martin Bialasinski <klingeling-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I would like to share a modification to bindAsEventListener() that > might be useful to some of you. It makes it possible to do > > function show(event, number) { > alert(Event.element(event).id + " = " + number); > } > > Event.observe("el1", "click", show.bindAsEventListener(this, 5); > Event.observe("el2", "click", show.bindAsEventListener(this, 10); > > That will alert "el1 = 5" when el1 is clicked and "el1 = 10" when el2 > is clicked. > > The changed function: > > Function.prototype.bindAsEventListener = function(object) { > var __method = this; > var argumente = $A(arguments); > argumente.shift(); // remove "object" from the list => just the > additional arguments remain > return function(event) { > var ar = argumente.slice(0); // copy values > ar.unshift(event || window.event); > return __method.apply(object, ar); > } > }The plain old Function.prototype.bind() provides this exact functionality. If you want to put that into bindAsEventListener, I''d recommend using the same variable names/techniques rather than reinventing them. Todd
On 2/28/06, Todd Ross <rails-spinoffs-25kFIyuv2iRiLUuM0BA3LQ@public.gmane.org> wrote:> The plain old Function.prototype.bind() provides this exact > functionality. If you want to put that into bindAsEventListener, I''d > recommend using the same variable names/techniques rather than > reinventing them.Well, it is basically the same technique, but it can be simplyfied a bit, I realized Function.prototype.bindAsEventListener = function(object) { var __method = this; var args = $A(arguments); args[0] = null; return function(event) { args[0] = event || window.event; return __method.apply(object, args); } }