views:

482

answers:

3

Hi all,

Pardon the complicated title. Here's my situation: I'm working on a Grails app, and using jQuery for some of the more complex UI stuff.

The way the system is set up, I have an item, which can have various files (user-supplied) associated with it. On my Item/show view, there is a link to add a file. This link pops up a jQuery modal dialog, which displays my file upload form (a remote .gsp).

So, the user selects the file and enters a comment, and when the form is submitted, the dialog gets closed, and the list of files on the Item/show view is refreshed. I was initially accomplishing this by adding

onclick="javascript:window.parent.$('#myDialog').dialog('close');"

to my submit button.

This worked fine, but when submitting some larger files, I end up with a race condition where the dialog closes and the file list is refreshed before the new file is saved, and so the list of files is out of date (the file still gets saved properly).

So my question is, what is the best way to ensure that the dialog is not closed until after the form submit operation completes? I've tried using the <g:formRemote> tag in Grails, and closing the dialog in the "after" attribute (according to the Grails docs, the script is called after form submission), but I receive an error (taken from FireBug) stating that

window.parent.$('#myDialog').dialog is not a function

Is this a simple JavaScript/Grails syntax issue that I'm missing here, or am I going about this entirely wrong?

Thanks so much for your time and assistance!

A: 

To avoid the race condition you mentioned, I would run the "refresh the file list" code after the submitted page has returned. I am not familiar with Grails, but this psuedoCode should give you an idea:

if (file was uploaded successfully){
 print "<html>
  <body onload='code_refresh_file_list;window.parent.$(\"#myDialog\").dialog(\"close\");'>
     file uploaded successfully...
  </body>
 </html>";
}else{
 print "<html>
  <body>
     could not upload file due to [reason], please re-upload the file
  </body>
 </html>";
}

Notice the body onload='' on the first part of the conditional...

Hope that helps

pǝlɐɥʞ
A: 

I'm a bit of a newbie and don't know Grails either but I've been doing straight JQuery ajax calls as shown below. It does seem like you're going about this wrong - forget about the click event and close the dialog after you get a response back from the server that the file has been successfully saved.

In the code below, the function receives something back from the server, string, json, whatever, (this is the response), and you do something with it if you want, like show a message and close your dialog.

$('#myForm').submit(function() {

   $.post($(this).attr('action'), $(this).serialize(), function(response) {
          myDialog('close'); // pseudo code here
          $('message').html(response);

});

return false;

});

BTW, you might embrace JQuery entirely and eliminate that javascript mixed in with your html.

BillB
A: 

May be it is happening because of library for jquery modal was not availble when dialog close was being called. Try this

jQuery(document).ready(function() {

  jQuery('#myDialog').dialog('close');

});

Amit Jain