moondog
moondog

Reputation: 1577

different 'click' callbacks in different scripts: can I control order of execution?

I have one script that manages playlist clips. It includes a callback that specifies what to do with the playlist when the user clicks on a clip:

$('div.clip').bind("click.playlist", function() {
  ...
});

I am now planning to add jquery-tabs, and would like to create a second script with a second callback, that specifies what to do with the tab content when the user clicks on a clip:

$('div.clip').bind("click.tabs", function() {
  ...
});

Here is my question: When the user clicks on a clip, I want the "click.tabs" callback to execute before the "click.playlist" callback. Does anyone know how to do this?

Upvotes: 1

Views: 101

Answers (3)

Ruan Mendes
Ruan Mendes

Reputation: 92274

The order that events handlers on the same element run is not guaranteed. Don't rely on it, this is true in most event systems, they are designed to be decoupled. If you really need it, you need to set a single handler that calls both functions you need in order.

Instead of

$('div.clip').bind("click.playlist", function() {
  doSomething();
});


$('div.clip').bind("click.tabs", function() {
  doSomethingElse();
});

Do

$('div.clip').bind("click", function() {
  doSomething();
  doSomethingElse();
});

Upvotes: 1

TheHe
TheHe

Reputation: 2972

imho i would do that in an other way:

$('div.clip').bind("clip.playlist", function() {
  ...
});

and

$('div.clip').bind("clip.tabs", function() {
  ...
});

and then fire it up with:

$('div.clip').click(function(){
    $(this).trigger('clip.playlist').trigger('clip.tabs');
});

to get the FULL control of event-chaining.

Upvotes: 3

jtheman
jtheman

Reputation: 7491

What if you used

$('div.clip').click(function()
{
    func1().func2()
});

Upvotes: 0

Related Questions