format.js没有操纵DOM一次动作缓存启用 [英] format.js is not manipulating dom once action caching enabled
问题描述
注:我是presenting逻辑在这里我在做什么的
我在做什么:
想想基本指标的行动,我们的上市产品,并与分页。现在,使用远程真选项,我已经启用基于AJAX的分页。到目前为止,事情工作完全正常。取样品code一起来看看。
Think about the basic index action where we are listing products and with pagination. Now using remote-true option I have enabled ajax based pagination. So far things works perfectly fine. Take a look on sample code.
产品控制器:
def index
@products = Product.paginate(:order =>"name ASC" ,:page => params[:page], :per_page => 14)
respond_to do |format|
format.html # index.html.erb
format.json { render json: @products }
format.js
end
end
Index.html.erb
Index.html.erb
<h1>Products</h1>
<div id="products">
<%= render "products/products" %> // products partial is just basic html rendering
</div>
<script>
$(function(){
$('.pagination a').attr('data-remote', 'true')
});
</script>
index.js.erb的
Index.js.erb
jQuery('#products').html("<%= escape_javascript (render :partial => 'products/products' ) %>");
$('.pagination a').attr('data-remote', 'true');
那么什么问题:
现在我想启用这个动作缓存。但index.js.erb的文件没有操纵DOM。如果删除了远程真正的功能那么事情正常工作与缓存。
Now I want to enable action caching on this. But index.js.erb file is not manipulating DOM. If I remove the remote-true functionality then things works fine with caching.
有关动作缓存我加入了这条线在控制器的顶部:
For action caching I have added this line on the top of the controller:
caches_action :index, :cache_path => Proc.new { |c| c.params }
有什么建议?
更新:
但问题是jQuery的code不执行。从这个问题
Problem is jquery code is not executing. From this question
我发现了什么是错误的。 jQuery的实际环绕传入脚本以使浏览器计算传入code。但缓存mechansim仅仅是节省了code为文本,当一次重新请求,它返回code为文本,但不进行评估。因此,需要EVAL的code明确的
但是,如何解决这个问题?
推荐答案
我看不出有什么问题,应该是使用远程:真正的
。别人建议使用阿贾克斯
而不是远程:真正的
,而这正是遥控功能呢,所以有应该是没有什么区别。
I don't see what the issue should be with using remote: true
. Someone else suggested to use .ajax
instead of remote: true
, but that's exactly what the remote functionality does, so there shouldn't be any difference.
其他答案有明确使用code jQuery.ajax
,但在他们的$ C C相比有什么遥控功能确实是$唯一的区别他们指定明确的的dataType
。实际上,你可以做到这一点与远程:真正的
虽然
The other answer has code that explicitly uses jQuery.ajax
, but the only difference in their code compared to what the remote functionality does is that they're specifying an explicit dataType
. You can actually do that with remote: true
though.
在你的HTML链接,你只需要指定数据类型=脚本
。或者,根据您发布的JS,你应该这样做:
In your HTML link, you just need to specify data-type="script"
. Or, based on your posted JS, you'd do this:
$(function(){
$('.pagination a').attr('data-remote', 'true').attr('data-type', 'script');
});
编辑:另外,我更深入的有关数据类型的属性,并写了它是如何工作使用Rails此处的http://www.alfajango.com/blog/rails-3-remote-links-and-forms-data-type-with-jquery/
这篇关于format.js没有操纵DOM一次动作缓存启用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!