是否可以使用Ajax频繁刷新部分内容? [英] Is it possible to refresh partial frequently using Ajax?
问题描述
在后台,我希望它重新加载并显示有多少未读邮件.
我想要没有刷新的页面.我的意思是使用ajax.
In background, I want it to reload and shows the number how many unread messages are there.
I want that without refreshing page. I mean using ajax.
如果我在菜单中有此菜单,如何每30秒仅刷新一次此部分?
If I had this in menu, how can I refresh only this section every 30 secs?
<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
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
更新: _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _
UPDATE:_______________________________
assets/javascript/refresh_messages_count.js
assets/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
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
views/layouts/_menu.html.erb
views/layouts/_menu.html.erb
<span id="message_received_count"><%= render :partial => "layouts/message_received_count" %></span>
views/layouts/_message_received_count.html.erb
views/layouts/_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 %>
views/messages/refresh_part.js.erb
views/messages/refresh_part.js.erb
$('#message_received_count').html("#{escape_javascript(render 'layouts/messages_received_count', data: @message_count)}");
推荐答案
您将使用setInterval发送ajax请求:
You will use setInterval to send the ajax request:
$(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"
})
}
然后您在控制器中执行以下操作:
Then you make an action in a controller like this:
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文件(可以用erb代替haml).
then you will write a js file named refresh_part.js.haml
(you could erb instead of haml).
refresh_part.js.haml看起来像这样:
refresh_part.js.haml would look like this:
$('#part_you_want_to_refresh').html("#{escape_javascript(render 'name_of_partial', data: @data)}");
确保在routes.rb
中设置正确的路由.
make sure you set the correct routes in routes.rb
.
这篇关于是否可以使用Ajax频繁刷新部分内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!