views:

1070

answers:

2

Hello,

I'm having a workflow issue with my JQuery dialogs when trying to create the dialogs and then doing a partial page render. I'll try to go through a sample scenario, and my apologies in advance for the long problem description:

The page loads, with the html that I would like to be turned into JQuery dialogs. The dialogs are created on document.ready (using .dialog()), but the autoOpen property is set to false. When JQuery creates the dialogs (if I'm using Firebug to inspect the page), the dialog html is actually stripped from its normal location and stuck at the very end of the document, with some wrapper classes around it. The user opens the dialogs by clicking a link that just does a $dialogDiv.dialog('open').

So that all works fine. The problem is that there are times when I am doing a partial page reload using AJAX (using ASP.NET MVC RenderPartial). The part of the page I'm refreshing happens to have all of the dialog html in it, so that gets re-written out. But remember the dialog (with all of the JQuery wrapper classes, etc) is already there a the bottom of the document. That html wasn't part of the page refresh, so now I'm stuck with two sets of dialog html. This is giving me all sorts of problems because I have duplicate id's on the page, and the jQuery behavior on these html elements becomes unpredictable. It's even worse when I start doing 3, 4, 5 partial page refreshes, because then I have 3, 4, 5 sets of dialog html (with only one real dialog having been made on document.ready).

I'm thinking that I might need to destroy the dialogs or something at some point, but I haven't had any luck with this approach. Does anyone have any ideas?

Thanks very much.

+5  A: 

According to the docs destroying a dialog removes dialog functionality and restores it to it's pre-init state but does not remove it from the DOM.

Since you are replacing the content and the dialog you could just remove the old ones.

$.ajax({
  url: '/some/url/',
  success:function(data){
    $('.ui-dialog').empty().remove();
    //add the new html and make the dialogs
  }
});

In response to you comment

I haven't seen you code, so I'm not exactly sure how you're settings up the dialogs, but in a general sense I would populate a variable with only the dialogs that will be replaced.

//inside document.ready
  var myDialog=$('#myDialog').dialog(),
  myOtherDialog=$('#myOtherDialog').dialog(),
  permanentDialog=$('#permanentDialog').dialog(),
  destroyableDialogs=[myDialog, myOtherDialog];

//ajax callback
success: function(data){
  $.each(destroyableDialogs, function(i,n){
    n.empty().remove();
  });
}
czarchaic
@czarchaic, Thanks for the response. You may have been the only person who took the time to read through the whole question. A follow-up question, though.. I have other dialog windows like error and alert dialogs that I would not want to get rid of. These would get emptied and deleted as well using the method you gave. And looking at the dialogs JQuery makes, it gives the outer wrapper the same list of classes on each, which is `ui-dialog ui-widget ui-widget-content ui-corner-all ui-draggable`. How can I specify which dialogs to empty and remove and which ones to not? Thanks again.
BAHDev
@BAHDev. I will update my post.
czarchaic
A: 

Same Problem, and the solution to remove all dialogs by hand doesn't go here. I have a framework that inserts, updates, destroys and refreshes ajax panels on my page. When any of this panels opens a ui dialog, this dialog stays FOREVER on my page.... Any dialog alternatives??? Any serious workarounds??

Minos