10

I was working the draggable plugin fine while using jQuery-UI 1.8.2, then I changed to 1.10.1. The major difference I found was that in enabling and disabling the plugin, I no longer needed to use:

$this.draggable('option', 'disabled', true);

but could simply use

$this.draggable('disable');

But then I realized there's another problem. I get this error, which messes up my entire program, and I don't know how to fix it:

Error: cannot call methods on draggable prior to initialization; attempted to call method 'enable'

To fix it, I ensured that I always call $this.draggable('enable'); before any further options, but it didn't make a difference. What's the problem?

user961627
  • 12,379
  • 42
  • 136
  • 210
  • 2
    Your error says `$this.draggable('enable');` is called before `$this.draggable();`. Have you checked the execution flow ? – LeGEC Feb 19 '13 at 11:01
  • Yep, I'd made a mistake in understanding the flow, I was able to make sure I did `$this.draggable()`. – user961627 Feb 19 '13 at 14:15

3 Answers3

18

The meaning of your error is : $this.draggable('enable'); is called before $this.draggable();.

Check the execution flow of your progam : make sure that you have indeed initialized the plugin (e.g : called $this.draggable();) before trying to do anything with it.

LeGEC
  • 46,477
  • 5
  • 57
  • 104
  • 2
    This is not really an answer to the question. It is a confusing rhetorical question. I think it should be rephrased to be an answer more directly. – Goodword Feb 28 '15 at 14:16
4

Expanding on what LeGEC said...

$this.draggable(); is being called before $this.draggable('enable');

For me the solution would be to chain the event like this...

$this.draggable().draggable('disable');

First declaring that $this is a draggable, then declaring that it is dissabled

2

I had a similar issue when upgrading from jquery 1.6.1 to 1.9.1

var tr$ = $('<tr>', { draggable: 'true' }); 

threw "cannot call methods on draggable prior to initialization"

modified to:

var tr$ = $('<tr>');
if(!('draggable' in document.createElement('span'))) {
  //handle old browsers                
} else {
  tr$.attr('draggable', 'true');
}

Posting in case it helps someone else to see it this way.

htrufan
  • 251
  • 1
  • 3
  • 12