Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery UI dialog - check if exists by instance method

I'd like to use instance method for testing if jQuery UI Dialog widget has been initialized or not. Regarding to API, this is possible, but it doesn't work for me:

Uncaught Error: cannot call methods on dialog prior to initialization; attempted to call method 'instance'

demo: http://jsfiddle.net/mDbV7/

UPDATE:

This was a mistake in the documentation, instance method will be available from version 1.11.0, see this issue.

like image 355
dmnc Avatar asked Apr 02 '13 12:04

dmnc


2 Answers

The latest version of jQuery UI no longer allows you to call UI methods on items that are not initialized yet. I've just been wrapping them in an if statement, like:

if ($("#divToBeDialoged").hasClass('ui-dialog-content')) {     // do whatever } else {     // it is not initialized yet } 

Edit: changed class name, thanks @dmnc

like image 136
jbabey Avatar answered Sep 20 '22 18:09

jbabey


It is also a good habit to empty and destroy dialogs once you're done using them. I usually use this code in the close event of each dialog

$("#myDialog").dialog({     // other options     close: function(event, ui) {         $(this).empty().dialog('destroy');     } } 

That'd be my advice, rather than asking every time if a dialog exists in an instance make sure that each dialog cleans up after itself.

like image 39
Darwin Santos Arismendy Avatar answered Sep 19 '22 18:09

Darwin Santos Arismendy