1

I have several objects listening for a change event, but I need them to fire off in a designated order.

Is it possible to customize the bubble order of several binded objects in jQuery?

Trip
  • 26,756
  • 46
  • 158
  • 277
  • 1
    They will bubble upwards through the DOM, I believe. – Shmiddty Nov 13 '12 at 18:56
  • Can you set up an example to demonstrate what you want to happen? – Shmiddty Nov 13 '12 at 18:56
  • Nope. It only bubbles up. It would be possible to write something to fire things top down, but it would be better if you tell us why things need to be in that specific order of yours. – PeeHaa Nov 13 '12 at 18:58
  • 1
    Maybe this might help? http://stackoverflow.com/questions/290254/how-to-order-events-bound-with-jquery – Matt Zeunert Nov 13 '12 at 18:59
  • You could use deferred objects to get this effect, but i suspect *bubble* isn't really what you meant. – Kevin B Nov 13 '12 at 19:00

2 Answers2

1

As far as I've experienced, there is an array of handlers which stores all the handlers referring the assigned functions. So when ever you add a new function for an event, it goes at the end of that array which means something like the code below:

$("SomeElement")
.live(
    {
        click: function ()
        {
            // This is handler named A.
            alert("A");
        }
    }
);

$("SomeElement")
.live(
    {
        click: function ()
        {
            // This is handler named B.
            alert("B");
        }
    }
);

B will be shown after A when "click" event triggered.

But when you change the places:

$("SomeElement")
.live(
    {
        click: function ()
        {
            // This is handler named B.
            alert("B");
        }
    }
);

$("SomeElement")
.live(
    {
        click: function ()
        {
            // This is handler named A.
            alert("A");
        }
    }
);

B will be shown before A when "click" event triggered.

You can order the adding handlers this way because the javascript code executes from in an up-down direction and I believe it's the only way.

Hope it helps.

Cheers

Rikki
  • 3,338
  • 1
  • 22
  • 34
1

Unfortunately no, you cannot customize event bubble order.

To achieve the effect you desire, I would recommend jQuery custom events.

Example:

$("#YOURELEMENT").trigger('CUSTOMEVENT'); //create a new custom event
$("#YOURELEMENT2").bind('CUSTOMEVENT',function(e){ ... }); //listen for new custom event

Here's a good tutorial on it.

adamb
  • 4,815
  • 1
  • 25
  • 40
  • 1
    So the custom event would toggle which action was performed first through indexing. Clever architectural idea here.. – Trip Nov 13 '12 at 20:13