是否可以使用Ajax频繁刷新部分?

时间:2012-12-21 10:41:53

标签: ruby-on-rails ruby-on-rails-3 jquery

在后台,我希望它重新加载并显示有多少未读消息的数量 我希望没有刷新页面。我的意思是使用ajax。

如果我在菜单中有这个,我怎么能每30秒刷新一次这个部分?

<li><%= link_to sanitize('<i class="icon-envelope"></i> ') + "received messages" + sanitize(' <span class="badge badge-info">'+current_user.mailbox.inbox(:read => false).count(:id, :distinct => true).to_s+'</span>'), messages_received_path  %></li>

messages_controller.rb

  def received
    if params[:search]
     @messages = current_user.mailbox.inbox.search_messages(@search).page(params[:page]).per(10)
    else
     @messages = current_user.mailbox.inbox.page(params[:page]).per(10)
    end 
     add_crumb 'Messages Received', messages_received_path

     @box = 'inbox'
     render :index
  end

更新: _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _

资产/ JavaScript的/ refresh_messages_count.js

$(document).ready(function () {
    // will call refreshPartial every 3 seconds
    setInterval(refreshPartial, 3000)

});

function refreshPartial() {
  $.ajax({
    url: "messages/refresh_part";
 })
}

messages_controller.rb

def refresh_part
    @message_count = current_user.mailbox.inbox(:read => false).count(:id, :distinct => true)

    # get whatever data you need to a variable named @data 
    respond_to do |format| 
        format.js {render :action=>"refresh_part.js"} 
    end

end

视图/布局/ _menu.html.erb

<span id="message_received_count"><%= render :partial => "layouts/message_received_count" %></span>

视图/布局/ _message_received_count.html.erb

<% if user_signed_in? && current_user.mailbox.inbox(:read => false).count(:id, :distinct => true) > 0 %>
 <li><%= link_to sanitize('<i class="icon-envelope"></i> ') + "Received" + sanitize(' <span class="badge badge-info">'+@message_count.to_s+'</span>'), messages_received_path  %></li>
<% else %>
 <li><%= link_to sanitize('<i class="icon-envelope"></i> ') + "Received", messages_received_path  %></li>
<% end %>

视图/消息/ refresh_part.js.erb

$('#message_received_count').html("#{escape_javascript(render 'layouts/messages_received_count', data: @message_count)}");

3 个答案:

答案 0 :(得分:25)

您将使用setInterval发送ajax请求:

$(document).ready(function () {
    // will call refreshPartial every 3 seconds
    setInterval(refreshPartial, 3000)

});

// calls action refreshing the partial
function refreshPartial() {
  $.ajax({
    url: "whatever_controller/refresh_part"
 })
}

然后你在这样的控制器中做一个动作:

def refresh_part
  # get whatever data you need to a variable named @data
  respond_to do |format|
    format.js
  end
end

然后你会写一个名为refresh_part.js.haml的js文件(你可能会错误而不是haml)。

refresh_part.js.haml看起来像这样:

$('#part_you_want_to_refresh').html("#{escape_javascript(render 'name_of_partial', data: @data)}");

确保在routes.rb

中设置正确的路线

答案 1 :(得分:2)

仅供参考,refresh_part.js.erb如下所示:

$("#part").html("<%= escape_javascript(render 'partial', data: @data) %>");

而不是:

$('#part').html("#{escape_javascript(render 'partial', data: @data)}");

另外,我们可以使用“escape_javascript”的别名来简化:

$("#part").html("<%= j(render 'partial', data: @data) %>");

答案 2 :(得分:0)

是的,你可以

<html>
<head>
<script type="text/JavaScript">
<!--
function timedRefresh(timeoutPeriod) {
    setTimeout("location.reload(true);",timeoutPeriod);
}
//   -->
</script>
</head>
<body onload="JavaScript:timedRefresh(5000);">
<p>This page will refresh every 5 seconds. This is because we're using the 'onload' event to call our function. We are passing in the value '5000', which equals 5 seconds.</p>
<p>But hey, try not to annoy your users too much with unnecessary page refreshes every few seconds!</p>
</body>
</html>

来源:http://www.quackit.com/javascript/javascript_refresh_page.cfm