2009-06-08 6 views
1

의 일부 내 link_to_remote 발전기를 AJAX 호출이 브라우저의 뒤로 버튼과 관련된 문제로 변환 중입니다. (어리석은 멍청한 실수!)link_to는 다음과 같습니다 : before 및/or : after after link_to_remote

내 응용 프로그램에는 여러 가지 이유로 대기 사이클이 긴 페이지가 여러 개 있습니다. 나는 사용자가 기다리는 동안 회 전자를 표시하기 위해 :before:after 호출을 사용하는 link_to_remote의 기능에 의존해 왔습니다. link_to도 비슷한가요? 나는 내가 link_to_remote 인 것처럼 전화를 던질 것으로 가정하고 있었지만, 실제로는 :before 전화에 대해서만 의미가 있습니다 (그리고 작동하지 않는 것 같습니다). 이 시점에서 내 다른 옵션은 무엇입니까?

+0

참조 : http://stackoverflow.com/questions/18447186/rails-link-to-do-something-after-confirmation/18449331 – Yarin

답변

4

LINK_TO "저를 클릭", your_url : onclick을 => "자바 스크립트"조금 자바 스크립트를 배울 의향이 있다면, 빠르고 쉽게 할 jQuery를에서 같은 물건을 찾을

+0

물론! 나는 바보처럼 느껴진다. 감사. –

1

link_to는 사용자가 클릭 할 수있는 간단한 앵커를 생성하므로 아마도 document.onunload에 대한 자바 스크립트 처리기를 추가하여 처리기 전에 :를 대체하는 것이 가장 좋습니다. 당신은 for : after 핸들러 ;-) Prototype의 Event.observe()를 확인하거나 Rails를 사용하여 Rails 통합을 향상시키는 것과 같은 일을한다.

0

. 여전히 AJAX 호출을하고있는 것처럼 들리지만, link_to_remote 동작은 불행한 부작용이있었습니다.

레일스 코드 보는 경우는 다음과 같습니다

<%= link_to "Show me", slow_page_url, :class => 'remote', :'data-update' => 'display' %> 

내가 이렇게 같은 jQuery를 블록을 추가 할 : 다음

<% content_for :dom_ready do %> 
    // register a callback for the click event on links with class 'remote' 

    $('a.remote').click(function() { 

    // this is the clicked_link, which you may want to 
    // persist thru several levels of callbacks 
    // so assign it to a local variable 

    var clicked_link = this; 

    // assuming you already have a start_spinner function, 
    // and you might pass it the object where the click occured 

    start_spinner(clicked_link); 

    // here's the ajax call, which will automatically update 
    // the div you identified using the data-update attribute 
    // with the returned html 

    $('#'+$(this).attr('data-update')).load($(this).attr('href'), {}, function() { 

     //this callback only runs when the AJAX request has completed 

     stop_spinner(clicked_link); 

    }); 

    // prevents the click event from propagating to the window object 
    //and moving the user away from the current page 

    return false; 
    }); 
<% end %> 

을 나는 내 레이아웃의 하단에 내 모든 자바 스크립트로드가 좋아해

<%= javascript_include_tag 'jquery-1.3.2.js' %> 
<script type="text/javascript"> 
$(function() { 
    <%= yield :dom_ready %> 
); 
</script>