views:

631

answers:

3

Hi

I have a problem I am using jquery U.I tabs that load everything with ajax. Now I have it right now everytime you click on a tab a partial view is loaded up into that tab.

Now in this partial view their are javascript files that use jquery to bind all the events that are needed in that tab plus some jquery plugins I am using.

Now every time that tab is loaded up all those scripts are loaded up. If it is clicked 10 times then those scripts are loaded 10times up meaing now each of say my buttons will now have 10 of the same events on it mean if someone clicks on that button 10 events will all fire off to and do the same thing.

So I need to find some solution to either move all the script out and have it on the main page and use jquery.live or some other solution.

I tried to do use jquery caching for the U.I tabs but this won't work since some things in say Tab A when changed effect Tab B meaning I need Tab B to be reloaded but the scripts can't reload otherwise I run into the same problem as now.

+2  A: 

i always, as a rule, call unbind() before bind()

Scott Evernden
I am not following? can I see some examples? How does that work with plugins? Is it fast or slower to do this then "live"?
chobo2
Also does there away just to unbind everything?
chobo2
calling unbind() before remove() and bind() is a good way to prevent multiple bindings on the same element. I prefer live() if something is created and deleted a lot (like animated hover effect on buttons)
Mike
But how about controls that are bound by plugins? I know some plugins have destroy and stuff like that but many do not.
chobo2
What I also don't get is that why is it only certain things are causing trouble. Like everything is getting rebinded X amount of times so why it only some of them cause weird results like when you click on one button it does the action 10 times while other buttons only do their action once.
chobo2
A: 

Instead of using .live(), consider using event delegation instead. Listen for an event inside a container, then inspect the tag and attributes to decide how to handle it.

Much more efficient if you're adding a bunch of elements to the DOM.

ScottE
I will need some examples since I am not following.
chobo2
here's a quick link - scroll down a bit http://www.learningjquery.com/2008/03/working-with-events-part-1
ScottE
I am not sure what I should be looking at.
chobo2
here, try this link , or google 'jquery event delegation' http://www.danwebb.net/2008/2/8/event-delegation-made-easy-in-jquery
ScottE
Scott, jQuery live() is in fact a form of event delegation. The only difference is that you do not pick the container (the one you register the event with) yourself, but jQuery does that for you.
Tom Bartel
+2  A: 

Read here for a nice presentation covering jQuery's event delegation.

After reading the above look at this:

// using jQuery 1.4.2
(function($){
    // You can cache the container you plan on using for event delegation.
    //   If using $(document).ready(...) to call the below functions
    //     your "container" element should already exist in the DOM
    //   (the .specialAjaxLink elements don't have to exist yet).
    var container = $("#container");

    // whenever an element with the class "specialAjaxLink" is clicked
    // while inside your container element execute the handler on them.
    container.delegate(".specialAjaxLink", "click", function(){
        // do something amazing...
        solveWorldHunger(this);
        // stop propagation and prevent default...
        return false;
    });
}(jQuery));

You shouldn't have to worry about performance problems when using the above method unless you are creating an app as complex as gmail, docs, or google wave.

David Murdoch