简体   繁体   English

rails link_to:remote

[英]rails link_to :remote

I have the following: 我有以下内容:

<%= link_to my_path, method: :delete, confirm: 'Delete?', class: 'link-delete', 'data-message' => 'Are you sure?', 'data-severity' => 'danger', :remote => true do %>
  <i class="icon-trash"></i>
<% end %>

which brings up a Bootstrap Modal for confirmation, and I wanted to hook onto the ajax call that so that I can display a spinner or some kind of text. 它会启动一个Bootstrap Modal进行确认,我想挂钩ajax调用,以便我可以显示一个微调器或某种文本。

I know that I can use unobtrusive javascript to listen to the click event like so, if I DON'T use ':remote => true' in my link_to 我知道我可以使用不引人注目的javascript来监听点击事件,如果我不在我的link_to中使用':remote => true'

jQuery ->
  $('.link-delete').live 'click', (event) ->
    $('.link-delete').html("Loading...")  #THE MSG OR ANIMATION I WANT TO DISPLAY
    $.get(this.href, null, null, 'script')
   false

but not sure how to combine the two when using ':remote => true' 但是在使用':remote => true'时不确定如何将两者结合起来

Any suggestions? 有什么建议?

thanks for the help 谢谢您的帮助

You can bind to ajax calls like this: 你可以像这样绑定到ajax调用:

<%= link_to my_path, method: :delete, confirm: 'Delete?', class: 'link-delete', 'data-message' => 'Are you sure?', 'data-severity' => 'danger', :remote => true do %>
  <i class="icon-trash"></i>
<% end %>

$('.link-delete').bind('ajax:beforeSend', function() {
  $('#mySpinner').show();
});

$('.link-delete').bind('ajax:complete', function() {
  $('#mySpinner').hide();
});

You don't need to combine the two. 你不需要将两者结合起来。 Just use the format.js to call the javascript. 只需使用format.js来调用javascript。

In your controller: 在你的控制器中:

Controller 调节器

 def my_method
     #code here
     respond_to do |format|
      format.js  {}
     end
    end

my_method.html.erb my_method.html.erb

<div id = "link-delete"></div>

my_method.js.erb my_method.js.erb

$("#link-delete").html("<%= escape_javascript(render(:partial => "text_message"))%>");

_text_message.html.erb _text_message.html.erb

<p>Loading...</p>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM