2009-04-17 49 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(); 
    })} 
); 
相关问题