2011-07-18 27 views
1

我想在完成FadeOut效果後才能進行ajax調用。 這是我簡單的代碼:Rails - 僅在視覺效果完成後才調用Ajax

<script type="text/javascript"> 
function closeIt(id) { 
    $(id).slideUp();   
} 
</script> 

<%= link_to_remote('Delete', :url => {:action => "ajaxdestroy", :controller => "blog_comments", :blog_post_id => @blog_post.id, :id => comment.id}, :before => "closeIt('comment#{comment.id}')", :update => "blogPostComments", :confirm => 'Are you sure?') %></p> 

現在生成的HTML看起來像這樣:

<a onclick="if (confirm('Are you sure?')) { closeIt('comment39'); new Ajax.Updater('blogPostComments', '/blog_comments/ajaxdestroy/39?blog_post_id=6', {asynchronous:true, evalScripts:true, parameters:'authenticity_token=' + encodeURIComponent('Xzvq2/0BlbVi3eVCxW5TGcKmap3nrVC1SiG76W+bVGc=')}); }; return false;" href="#">Delete</a> 

所以效果closeIt函數被調用,但效果基本show效果之前Ajax調用完成。如何在先前closeIt函數的回調函數中調用Ajax函數?

感謝

回答

2

使用

<a onclick="return closeThat(39);" href="#">Delete</a> 



function closeThat(id) { 
    if (confirm('Are you sure?')) { 
     closeIt('comment'+id, function() { 
      new Ajax.Updater('blogPostComments', '/blog_comments/ajaxdestroy/'+id+'?blog_post_id=6', { 
       asynchronous: true, 
       evalScripts: true, 
       parameters: 'authenticity_token=' + encodeURIComponent('Xzvq2/0BlbVi3eVCxW5TGcKmap3nrVC1SiG76W+bVGc=') 
      }); 
     }); 
    } 
    return false; 
} 


function closeIt(id, callback) { 
    $(id).slideUp(400, callback);   
} 
+0

謝謝Christophe,但我怎麼能讓這個JavaScript代碼從rails代碼生成?實際上JavaScript代碼是自動生成的 – coorasse

+0

你是什麼意思「從rails代碼生成」? – alste

+0

<%= link_to_remote('Delete',:url => {:action =>「ajaxdestroy」,:controller =>「blog_comments」,:blog_post_id => @ blog_post.id,:id => comment.id},: before>>「closeIt('comment#{comment.id}')」,:update =>「blogPostComments」,:confirm =>'Are you sure?')%>生成javascript代碼。我可以使用:在符號之前定義一個在Ajax調用之前調用的javascript函數,但我不知道如何使Ajax調用被稱爲:before函數的回調函數 – coorasse

1

好吧,我解決了使用JavaScript創建後。我除去:前和:確認從link_to_remote符號,並增加了「ID」字段,以將所生成的元件

<%= link_to_remote 'Cancella', {:url => {:action => "ajaxdestroy", 
              :controller => "blog_comments", 
              :blog_post_id => @blog_post.id, 
              :id => comment.id}, 
           :update => "blogPostComments"}, 
           {:id => "delete#{comment.id}"} %></p> 

    <script> 
     var onClick<%=comment.id%> = new Function($('#delete<%= comment.id%>').attr('onClick')); //get the ajax call autogenerated   
     function nuova() { //define a new function to wrap it 
       if (confirm('Are you sure?')) { //move here the confirm message 
        $('#comment<%= comment.id%>').hide(600,onClick<%=comment.id%>); 
       } 
       return false;  
    }         
     $('#delete<%= comment.id%>').removeAttr('onclick').click(nuova); 
    </script> 

不乾淨但是這是一個解決方案。現在刪除ajax調用只在效果終止後提交

相關問題