How to use onLoading event in grails remoteFunction

I am making a web application in grails.In that I am using remoteFunction in gsp page.It is working now. That in the onloading event I want to call the showSpinner () javascript function. My gsp code example:

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

Can anyone help with this.

+3
source share
2 answers

You can register the so-called Ajax.Responder globally for the Prototype Ajax onLoading event. This will fire for every remoteFunction / Ajax call on your page. To do this, you should put something like this on the gsp page or 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>

, showSpinner hideSpinner. - :

<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>
+4

JQuery, :

$(document).ready(function() {
    $("#spinner").bind("ajaxSend", function() {
        $(this).fadeIn();
    }).bind("ajaxComplete", function() {
        $(this).fadeOut();
    })}
);
+3

Source: https://habr.com/ru/post/1706569/


All Articles