On May 27, 8:37 pm, Luisgo <lgo...@gmail.com> wrote:
> Hi all... I was about to submit a ticket (enhancement) but maybe this
> has come up before so I ask here... what do you think about being able
> to pass more than one event to 'observe' for cases where one may want
> the same behavior to run for more than one event?
>
> I realize this can be achieved by extracting the function and passing
> it to observe for each event but I want to compress it even more by
> doing something like:
>
> $$('input[type=text]').invoke('observe', ['keyup', 'blur'], function
> (event){
>   // some code ...
>
> });
>
> This would save me from doing:
>
> function doSomething(){
>   // some code ...}
>
> $$('input[type=text]').invoke('observe', 'keyup', doSomething);
> $$('input[type=text]').invoke('observe', 'blur', doSomething);
>
> Maybe this can already be done some other way that I am not aware of?

I would do something like:

$$('input[type=text]').each(function(el) {
  el.observe('keyup', doSomething)
     .observe('blur', doSomething);
});

You can also do some chaining with `invoke`, but that could be
inefficient (2 loops, instead of 1 when using `each`) and looks a
little weird:

$$('input[type=text]')
  .invoke('observe', 'keyup', doSomething)
  .invoke('observe', 'blur', doSomething);

I remember there was a proposal once to support adding multiple
observers for multiple events:

myElement.observe({
  keyup: onKeyup,
  blur: onBlur
});

which can currently be written as:

myElement.observe('keyup', onKeyUp).observe('blur', onBlur);

If we were to implement such syntax, you would be able to write your
expression like so (which, IMO, looks pretty clean):

$$('input[type=text]').invoke('observe', {
  keyup: doSomething,
  blur: doSomething
});

I suppose your idea does make sense. I'm indifferent to it, but maybe
someone else will like it. We can try using an alternative syntax such
as - "foo | bar" or "foo, bar" - to eliminate array object creation,
but that might be ambiguous and not back-compat., considering that
custom events are currently allowed to include those characters ("|"
and ",").

Does anyone find this addition handy or is not worth it?

--
kangax
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Prototype: Core" group.
To post to this group, send email to prototype-core@googlegroups.com
To unsubscribe from this group, send email to 
prototype-core-unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/prototype-core?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to