2009/5/28 grunk <roger.oliv...@gmail.com>:
>
> Wow ! Very instructive :)
> Thank's a lot
>
> On 27 mai, 17:32, "T.J. Crowder" <t...@crowdersoftware.com> wrote:
>> Hi,
>>
>> > So i was wondering if my code could be optimized ?
>>
>> Yes, you only need to do the binding once, and then you can reuse it.
>> There are actually several optimizations you can perform on that
>> code.  I'll take an iterative approach:
>>
>> 1. You don't need to bind your iteration function; Enumerable#each[1]
>> has a second parameter you can give it to set the context for the
>> iterator.  So (I also put in a missing closing parenthesis and
>> semicolon):
>>
>> // Take 1
>> initialize : function() {
>>     $$('.poper').each(function(element){
>>         Event.observe
>> (element,'click',this._clicCase.bindAsEventListener(this));
>>     }, this);
>>
>> }
>>
>> 2. As you suspected, you only hav to bind once
>>
>> // Take 2
>> initialize : function() {
>>     var f = this._clicCase.bindAsEventListener(this);
>>     $$('.poper').each(function(element){
>>         Event.observe(element,'click',f);
>>     });
>>
>> }
>>
>> 3. And you almost certainly don't need bindAsEventListener (you almost
>> never do[2]), you can use Function#bind[3]
>>
>> // Take 3
>> initialize : function() {
>>     var f = this._clicCase.bind(this);
>>     $$('.poper').each(function(element){
>>         Event.observe(element,'click',f);
>>     });
>>
>> }
>>
>> 4. AND you can ditch your iterator function entirely, because you can
>> use Enumerable#invoke[4]
>>
>> // Take 4
>> initialize : function() {
>>     $$('.poper').invoke('observe', 'click', this._clicCase.bind
>> (this));
>>
>> }
>>
>> [1]http://prototypejs.org/api/enumerable/each
>> [2]http://proto-scripty.wikidot.com/prototype:tip-you-probably-don-t-nee...
>> [3]http://prototypejs.org/api/function/bind
>> [4]http://prototypejs.org/api/enumerable/invoke
>>
>> HTH,
>> --
>> T.J. Crowder
>> tj / crowder software / com
>> Independent Software Engineer, consulting services available
>>
>> On May 27, 2:48 pm, grunk <roger.oliv...@gmail.com> wrote:
>>
>> > Hi,
>> > I'm using bindAsEventListener in my classes like this :
>> > <code>
>> > initialize : function(){
>> >     $$('.poper').each(function(element){
>> >         Event.observe
>> > (element,'click',this._clicCase.bindAsEventListener(this)
>> >     }.bindAsEventListener(this));}
>>
>> > </code>
>>
>> > in the prototype doc we can read : <quote> you must remember that
>> > bindAsEventListener returns a fresh anonymous function that wraps your
>> > method. This means that every call to it returns a new function </
>> > quote>
>> > So i was wondering if my code could be optimized ?
>> > For exemple, if i have 40 elements with the class ".poper"  , is the
>> > bindAsEventListener calls will slow down my app ?
>>
>> > Thank's :)
> >
>

If the 40 elements are in a container which is not too far up the DOM
tree, then you COULD attach the event handler to the container and
allow the events to bubble up to a single instance.

And then use something like ...

function checkboxToggles(ev) {
        var o_evl = ev.element();
        var o_CheckBoxToggler = o_evl.up('.checkboxToggler');

        // Don't perform any action if the toggles are disabled.
        if (!o_CheckBoxToggler.hasClassName('checkboxTogglerDisabled')) {
                // Process toggling of checkboxes (All On, All Off, All Toggle)

        }
}

I use this technique for dynamic content where I use an AJAX request
to retrieve values for checkboxes. The checkboxes are in groups
determined by the AJAX response. Each group has a small set of
"buttons" to toggle the checkboxes (on,off,toggle).

The id of the group determines the ids of the checkboxes (that's how
I've handled it).

So by watching the containers for the toggles, I know which group I am
interacting with.

I've only got 1 event handler attached.

So, I think this should be a pretty efficient way of working.

I'm sure others will comment on this. (I'm a newbie - ish so there may
be better ways).

-- 
-----
Richard Quadling
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
"Standing on the shoulders of some very clever giants!"

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

Reply via email to