问题
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