views:

267

answers:

1

I'm working with some existing javascript code which takes an LI and makes it droppable to an input field. I wanted to add an additional constraint to this, and enforce this by making my own event handler, and calling event.stopImmediatePropagation() if the constraint is not met. This existing javascript is look like this:

$input.droppable({
    accept: accept,
    activeClass: 'droppable_active',
    hoverClass: 'droppable_hover',
    //          tolerance:    'touch',
    drop: function(e, ui)
    {
       // do some stuff;
    }

And is initiated in my code via the following call:

$('form').dmFrontForm();

Which is a custom jQuery UI which sets that event handler.

Before I make this call, I setup my own handler:

$('input').('drop', function(e, ui) {
    if (constraintIsNotMet) {
        e.stopImmediatePropagation();
    }
});

I verified the order that the event handlers were being called by adding in alert statements (actually no matter what order I put these lines of code, my handler always responded first which is a bit fishy to me), but no matter how hard I try, I can't get the first handler to quit.

+1  A: 

I know this is not a direct answer to your question of why it does not stop the propagation, but why not use the built-in accept method ?

http://docs.jquery.com/UI/Droppable#option-accept

example of usage

$('.selector').droppable('option', 'accept', function(){...});

the function should return false to cancel the drop ..

Gaby
#1 I don't have control over the original javascript code (since its not mine)#2 accept looks like it takes a selector as a constraint. My constraint is complex and based on some logic
blockhead
accept takes either a selector or a function as a constrain .. and you can add it after the initialization of the droppable. look my revised answer.
Gaby
It works but I can't give feedback to my user since function seems to be called every second the thing is being dragged. Not my ideal situation, but it works for now.
blockhead
hmm.. it should not run constantly ... only when you start/stop the drag of something and when you enter/leave the dropable area... try using console.log(this); inside the accept function to monitor the invoking..
Gaby
Running it at the start is just as bad...that would mean the second they start dragging something they would get a notification, which is not something I want.
blockhead