How do i build html inside .js.haml file?

懵懂的女人 提交于 2019-12-24 10:58:33

问题


I am wondering how to eloquently return the following call in my ajax call? Where i can have break lines etc inside the append function?

$(".q#{@question_id}.result_to_response")
.html("#{escape_javascript(@resp)}")
.append('<div><a href="#" class="continue_button">Continuez to next question</a></div>');

回答1:


My advice to you is just use jQuery templates or smthng similar for that. This way you easily generate all your templates with haml and then just append .tmpl() return value in html body.

Generating inline javascript is a sign of poor application design.




回答2:


Haml isn't really suited for generating JavaScript; it's really only meant for HTML and XML (for which it's excellent).

Also, you shouldn't be generating your JS/CoffeeScript files dynamically at all. Your JS files should be static (like your Ruby files).

In addition, the fact that you're using escape_javascript in your HTML makes me suspicious that you're trying to generate inline JavaScript. If that's the case, don't do it. JS and CoffeeScript belong in static external files only.

If you can provide more information about your use case, I'll try to make concrete suggestions.



来源:https://stackoverflow.com/questions/10235954/how-do-i-build-html-inside-js-haml-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!