当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - How to improve speed execution of coffeescript in rails?

问题描述:

To increase speed of page loading I`ve implemented creation comments via AJAX. It is simple and not heavyweight.In controller action I have:

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

and js file:

$("<%= 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()

But instead of speed up performance I got the opposite effect. Response time through JavaScript increased on 100-200 ms(~300ms total). Is this normal behavior or I am doing something wrong? Is there any way to improve speed little bit?

My performance test:

UPD:

My performance test with just JS file.

网友答案:

Let's put aside for the moment my opinion that embedding ERB in CoffeeScript is utterly gross to look at and unmaintainable. It is indisputable that there's a huge performance impact when you generate and compile CoffeeScript on every request.

You also lose any chance at HTTP caching.

My first suggestion would be to separate CoffeeScript from ERB. Populate hidden fields in your ERB with the data you need, and then mine those fields in your CoffeeScript.

But if you must embed ERB in what should be static files, embed the ERB tags in pure JavaScript instead, and let the compiled CoffeeScript use those.

网友答案:

The code you've written wouldn't really be expected to speed up the request, because Rails still need to process all the ERB etc. You're still returning rendered HTML and sending it to the browser where it is added to the DOM.

If you wanted to make it 'faster' you could simply render the @response as json and deal with it on the client using jQuery or a front end framework.

The code does make it nicer for the user, however, because it doesn't refresh the entire page.

网友答案:

My suggestion would be to hook into the form request, and on success, render the new comment, else re-render form with errors. An example:

# app/javascripts/comments.coffee

$('#comment-form').bind 'ajax:complete', (data, status, xhr) ->
  // psuedo code
  1. if status is success
       append comment -- $('#comments').append(data.comment)
  2. else
       re-render form with errors -- $('#comment-form').html(data.form)
  1. Return comment template (comments/comment) and append to comments
  2. Update your controller to return the form with JS response if not_acceptable.
  3. Note the file is found in app/javascripts/comments.coffee
分享给朋友:
您可能感兴趣的文章:
随机阅读: