views:

1025

answers:

1

Hi, I am doing a web application in grails.In that I am using remoteFunction in gsp page.It is working now.In that in "onloading" event I want to call "showSpinner()" javascript function.My sample gsp code is :

<div class="menuButton" onclick="${remoteFunction(action: 'index', controller: 'file',     update: [success: 'ajax', failure: 'ajax'])}">
      <label class="menu">File upload</label>
  </div>

Can anyone provide help on this.

+3  A: 

You can register a so-called Ajax.Responder globally for the onLoading event of Prototype Ajax requests. This would fire for every remoteFunction/Ajax call in your page. To do that you should put something like this somewhere into your gsp page or the layout:

<script type="text/javascript">
function showSpinner() {
   // TODO show spinner
}
function hideSpinner() {
   // TODO hide spinner
}
Ajax.Responders.register({
   onLoading: function() {
      showSpinner();
   },
   onComplete: function() {
      if(!Ajax.activeRequestCount) hideSpinner();
   }
});
</script>

You'd need to implement the showSpinner and hideSpinner function of course. As a complete example, you could use something like:

<script type="text/javascript">
   function showSpinner() {
      $('spinner').show();
   }
   function hideSpinner() {
      $('spinner').hide();
   }
   Ajax.Responders.register({
      onLoading: function() {
         showSpinner();
      },
      onComplete: function() {     
         if(!Ajax.activeRequestCount) hideSpinner();
      }
   });
</script>
<div id="spinner" style="display: none;">
   <img src="${createLinkTo(dir:'images',file:'spinner.gif')}" alt="Loading..." width="16" height="16" />
</div>
Siegfried Puchbauer