如何在Rails中发回js.haml
我有一个rails create动作,该动作将文件中的一些jquery发送回去:
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();
我已经开始使用HAML,并且想知道我应该如何转换它.我可以使用js.haml吗? 如果是这样,标记应该是什么样?
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?
实际上,在HAML中返回JS非常简单,只需使用:plain过滤器并在#{}中包含要评估的任何内容即可.
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();
请勿使用:javascript过滤器,因为它会将所有不必要的标签括在标签中.
Do not use the :javascript filter as it will enclose everything in a tag which is unnecessary.
如果需要使用一些逻辑,只需将:plain放在嵌套中即可.
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')}");
对不起ERB.