Question

I've added both callbacks and triggered events to a plugin as follows:

// located at the point where the event and callback should be called
el.trigger('initialized', el);
if ($.isFunction(options.onInitialize)) { options.onInitialize(el); }

but I found another method where it was done this way:

// located at the beginning of the script, after the options are extended
if ($.isFunction(options.onInitialize)) { el.bind('initialized', options.onInitialize; }

// located at the point where the event should be called
el.trigger('initialized', el);

So, my question is does it matter that the event is triggered before the callback in the first method or should I switch to using the second method where they both occur at the same time?

Update: The only reason I've come up with so far is minimizing function calls - $.isFunction is only called once in the second example.

Was it helpful?

Solution

I ended up going with the second method. Since the $.isFunction() is called each cycle it just seems to be more efficient to only do it once. I could have cached the result as well... so I guess there is either method will work just as well.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top