2009-04-17 55 views
3

我做的,我在GSP page.It使用remoteFunction在grails.In Web應用程序正在now.In,在「onloading」事件中,我想打電話給「showSpinner() 「javascript函數。我的示例gsp代碼是:如何在Grails中使用onLoading事件remoteFunction

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

任何人都可以提供這方面的幫助。

回答

4

您可以爲原型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> 
3

如果使用jQuery插件,使用:

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