Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
621 views
in Technique[技术] by (71.8m points)

jquery - How to send back js.haml in rails

I have a rails create action which send back some jquery in a file:

create.js.erb

var appearance = $("<%= escape_javascript(render(:partial => @appearance)) %>").effect("highlight", {color: '#E6ff00'}, 2000);
$("#sortable").append(appearance);
$("#new_appearance")[0].reset();

I have started using HAML and want to know how I should be converting this. Can I use js.haml? If so what should the markup look like?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Actually, returning JS in HAML is pretty easy, just use the :plain filter and enclose anything you want evaluated in #{}.

:plain
  var appearance = $("#{escape_javascript(render(:partial => @appearance)}").effect("highlight", {color: '#E6ff00'}, 2000);
  $("#sortable").append(appearance);
  $("#new_appearance")[0].reset();

Do not use the :javascript filter as it will enclose everything in a tag which is unnecessary.

If you need to use some logic, just put the :plain inside the nesting.

- if params[:printing]
  :plain
    $('#print-view').html("#{escape_javascript(render 'print_preview')}");

Sorry ERB.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...