michal
michal

Reputation: 161

proper way of handling drop events in jQuery UI

I'm using jQuery UI droppable and draggable. Based on what element was dropped on some canvas different functions are invoked. So right now it's something like this:

$('#myCanvas').droppable({
    drop: function (e, ui) {
        if (draggableId === "idA") {
            functionA();
        } else if (draggableId === "idB") {
            functionB();
        } else if (draggableId === "idC") {
            functionC();
        }
    }
});

I wanna get rid of that ugly complexions. It looks like a polymorphism problem, but I don't really know how to handle this in such a case. Thanks for help!

Upvotes: 0

Views: 53

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388316

Try

var fns = {
    idA: functionA,
    idB: functionB,
    idC: functionC
}
$('#myCanvas').droppable({
    drop: function (e, ui) {
        var fn = fns[draggableId];
        if($.isFunction(fn)){
            fn();
        }
    }
});

Upvotes: 1

Related Questions