I have the following:
container.dialog().bind('dialogopen', function(event, ui)
{
...
if (someCondition)
{
$(this).dialog('close'); // the dialog is not closed!
}
}
How I should make it work?
Unfortunately there is no ‘beforeopen’ event to be hooked.
The problem here is you need to bind the event before it happens. Currently you’re calling
.dialog()which opens the dialog (unlessautoOpen: falseis a provided option). This means before your.bind(....)runs, the event already occured. The solution is to just bind before the event happens, like this:You can view a demonstration here, it’ll prevent the dialog from opening, or rather it does open, but instantly closes before the UI thread updates, so to the user, it never opened.
This works because the
dialogopenevent is fired on the element you turned into a dialog (not the dialog container)…it doesn’t matter that the dialog itself is created later, this is just a DOM element listening to events.