【发布时间】:2013-11-05 17:42:38
【问题描述】:
为了提高页面加载速度,我通过 AJAX 实现了创建 cmets。它很简单而不是重量级。在控制器操作中我有:
def create
@comment = @commentable.comments.new(params_comment)
respond_to do |format|
if @comment.save
flash.now[:notice] = "Your comment added."
@response = {comment: @comment, model: @commentable}
format.js { @response }
format.html { redirect_to @commentable }
else
format.js { render nothing: :true, status: :not_acceptable }
format.html { redirect_to @commentable, status: :not_acceptable }
end
end
end
和js文件:
$("<%= escape_javascript( render 'comments/comment', @response)%>").appendTo(".comments").hide().fadeIn(500)
$('.notice-wrapper').html("<%= j(render partial: 'shared/notice') %>")
$('.alert').fadeOut(3000)
if $(".no-comments").css("display") is 'block'
$(".no-comments").hide()
$(".new_answer").hide()
$(".open").show()
但是我得到了相反的效果,而不是提高性能。通过 JavaScript 的响应时间增加了 100-200 毫秒(总共约 300 毫秒)。这是正常行为还是我做错了什么?有什么办法可以提高一点速度吗?
我的性能测试:
更新: 我只使用 JS 文件进行性能测试。
【问题讨论】:
标签: javascript ruby-on-rails performance coffeescript