如何在grails remoteFunction中使用onLoading事件

时间:2009-04-17 04:38:03

标签: ajax frameworks grails

我正在用grails做一个web应用程序。我在gsp页面中使用remoteFunction。它现在正在工作。在“onloading”事件中我想调用“showSpinner()”javascript函数。我的示例gsp代码是:

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

任何人都可以提供帮助。

2 个答案:

答案 0 :(得分:4)

您可以为Prototype Ajax请求的onLoading事件全局注册所谓的Ajax.Responder。这将触发页面中的每个remoteFunction / Ajax调用。要做到这一点,你应该在你的gsp页面或布局中放置这样的东西:

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

答案 1 :(得分:3)

如果您使用JQuery插件,请使用:

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