1

I asked a similar question over a year ago. But I can't figure this out.

This works:

$('#PlayArea').droppable({
    drop: function( myEvent, myUI ) {
        debugger;
    }
});

But this doesn't:

function myDroppable() {
    this.drop = function( myEvent, myUI ) {
        debugger;
    }
};
$('#PlayArea').droppable(myDroppable);
Community
  • 1
  • 1
Phillip Senn
  • 46,771
  • 90
  • 257
  • 373

3 Answers3

2

droppable takes an object literal, not a function. These two snippets are not equivalent, what you're actually doing is something like this (it's easier to see it's wrong this way):

$('#PlayArea').droppable(function myDroppable() {
    this.drop = function( myEvent, myUI ) {
        debugger;
    }
});

If you want to use a named function, make it return the configuration object that's accepted by droppable.

kryger
  • 12,906
  • 8
  • 44
  • 65
2

You need to use, As droppable takes an object literal, thus return it.

function myDroppable() {
    return {
        drop: function (myEvent, myUI) {
            debugger;
        }
    }
};
$('#PlayArea').droppable(myDroppable());

OR

var myDroppable = {
    drop: function (myEvent, myUI) {
        debugger;
    }
};
$('#PlayArea').droppable(myDroppable);
Satpal
  • 132,252
  • 13
  • 159
  • 168
  • in this case, shouldn't you make the last line `$('#PlayArea').droppable(myDroppable());` so the function is actually called, instead of passed as the parameter? – forgivenson Jan 03 '14 at 16:59
  • Is there a way to say myDroppable.tolerance = 'pointer', using the equals sign as the assignment operator instead of the colon? – Phillip Senn Jan 03 '14 at 17:10
  • @Phillip, You can use `$("#PlayArea" ).on( "drop", function( event, ui ) {} )`. We have limitation as droppable accept object literal. So you can try `$("#PlayArea" ).on( "drop", new myDroppable().drop)` – Satpal Jan 03 '14 at 17:13
0
myDroppable = {};
myDroppable.drop = function( myEvent, myUI ) {
   debugger;
}
myDroppable.tolerance = 'pointer';
myDroppable.hoverClass = 'ui-state-highlight';
$('#PlayArea').droppable(myDroppable);

This way you don't use colons as assignment operators.

Phillip Senn
  • 46,771
  • 90
  • 257
  • 373