For a widget of mine I needed to a group of li elements to assign a
multiple events (note this is just part of the code so 'this'
reference to the widget:

revision 1:
var li = $$('#main li.views');
        li.each(function(el)
        {
                el.observe('mousedown', this.onpress.bind(this));
        }.bind(this));

        li.each(function(el)
        {
                el.observe('mouseup', this.onrelease.bind(this));
        }.bind(this));

revision 2: this as you see is long and dirty, so I rewrite it to be
more shorter and cleaner
$$('#main li.views').invoke('observe', 'mousedown',
this.onpress.bind(this)).invoke('observe', 'mouseup',
this.onrelease.bind(this));

revision 3: I decided revision 2 wasn't beautiful enough, so I
extended Element.Methods

Element.addMethods(
{
        multiobserve: function(element, events)
        {
                element = $(element);

                for(var ev in events)
                {
                        element.observe(ev, events[ev]);
                }

                return element;
        }
}

and a write this

$$('#main li.views').invoke('multiobserve',
{
        mousedown: this.onpress.bind(this),
        mouseup: this.onrelease.bind(this)
});

Note: I thought to do something like that:
Element.addMethods(
        observe: Event.observe.wrap(function (wrapper, element, eventName,
handler)
        {
                if (!handler)
                {
                        for(var ev in eventName)
                        {
                                Event.observe(element, ev, eventName[ev]);
                        }

                        return $(element);
                }

                return wrapper(element, eventName, handler);
        })
);
but I didn't want to extend the base observe method

Hope to be helpful for some one.

p.s. I haven't tried the trick from here ( http://pastie.caboo.se/87940
) to write
$$('#main li.views').multiobserve(
{
        mousedown: this.onpress.bind(this),
        mouseup: this.onrelease.bind(this)
});


--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/prototype-core?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to