12

Maybe I'm totally missing something about even handling in jQuery, but here's my problem.

Let's assume there are some event binding, like

$(element).bind("mousemove", somefunc);

Now, I'd like to introduce a new mousemove binding that doesn't override the previous one, but temporarily exclude (unbind) it. In other words, when I bind my function, I must be sure that no other functions will ever execute for that event, until I restore them.

I'm looking for something like:

$(element).bind("mousemove", somefunc);
// Somefunc is used regularly
var savedBinding = $(element).getCurrentBinding("mousemove");
$(element).unbind("mousemove").bind("mousemove", myfunc);
// Use myfunc instead
$(element).unbind("mousemove", myfunc).bind("mousemove", savedBindings);

Of course, the somefunc is not under my control, or this would be useless :)

Is my understanding that is possible to bind multiple functions to the same event, and that the execution of those functions can't be pre-determined. I'm aware of stopping event propagation and immediate event propagation, but I'm thinking that they are useless in my case, as the execution order can't be determined (but maybe I'm getting these wrong).

How can I do that?

EDIT: I need to highlight this: I need that the previously installed handler (somefunc) isn't executed. I am NOT defining that handler, it may be or may be not present, but its installed by a third-party user.

EDIT2: Ok, this is not feasible right now, I think I'm needing the eventListenerList, which is not implemented in most browsers yet. http://www.w3.org/TR/2002/WD-DOM-Level-3-Events-20020208/changes.html

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
AkiRoss
  • 11,745
  • 6
  • 59
  • 86

2 Answers2

7

Another way could be to use custom events, something along these lines:

var flag = 0;
$(element).bind("mousemove", function() {
    if(flag) {
        $(this).trigger("supermousemove");
    } else {
        $(this).trigger("magicmousemove");
    }
}).bind("supermousemove", function() {
    // do something super
}).bind("magicmousemove", function() {
    // do something magical
}); 

$("#foo").click(function() {
    flag = flag == 1 ? 0 : 1; // simple switch
});

Highly annoying demo here: http://jsfiddle.net/SkFvW/

karim79
  • 339,989
  • 67
  • 413
  • 406
  • Oops, sorry, I was wrong: that solution doesn't work, because I need to disable the previous mousemove handler that was binded. – AkiRoss Aug 05 '10 at 00:31
  • @AkiRoss - Why do you need to disable the previous handler? By flipping the switch, you get the same effect. – JasCav Aug 05 '10 at 00:38
  • @Jason, because if a user binds a function to an event and I'm introducing new events, I may need to intercept that event and trigger another event without letting the original function to be executed. – AkiRoss Aug 05 '10 at 00:55
0

Good if the event is bound to multiple elements:

$('.foo').click(function() {
    if ( ! $(this).hasClass('flag')) {
        do something
    }
});

(add class 'flag' to sort of unbind, add it to 'bind')

Bob
  • 5,809
  • 5
  • 36
  • 53