2009-06-08 76 views
1

由于AJAX调用正在使用我的浏览器后退按钮创建问题,因此我正在将一些link_to_remote生成器转换为。 (愚蠢的noob错误!)不会link_to在:之前和/或之后:link_to_remote

我的应用程序有很多页面有很长的等待周期(由于各种原因失控)。我一直在依靠link_to_remote的使用:before:after通话,而用户正在等待显示微调能力。 link_to有类似的东西吗?我是假设,只是折腾像我是link_to_remote的电话......但这真的只让对:before呼叫读(和它似乎并不奏效)。我现在还有什么其他选择?

+0

另请参阅:http://stackoverflow.com/questions/18447186/rails-link-to-do-something-after-confirmation/18449331 – Yarin 2013-08-26 18:38:07

回答

4

的link_to“点击我”,YOUR_URL,:的onclick =>“的JavaScript”

+0

当然!我觉得自己像个白痴。谢谢。 – 2009-06-09 16:36:36

1

的link_to以来生成一个简单的锚用户点击,可能是你最好的选择是添加JavaScript处理程序document.onunload到您的替换:处理程序之前。你SOL为:后处理;-)对于此次检查出原型的Event.observe(),或使用RJS具有更好的Rails集成了同样的事情。

0

如果你愿意学习一些JavaScript,我发现做同样的东西,在jQuery的是快速和容易。这听起来像你还在做AJAX调用,只是link_to_remote行为有不幸的副作用。

如果你的Rails视图代码如下所示:

<%= 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 load像这样

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