1

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>');
Kamilski81
  • 14,409
  • 33
  • 108
  • 161

2 Answers2

0

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.

icanhazbroccoli
  • 1,035
  • 9
  • 15
  • If you mean "poor" rather than "pure", then I agree. – Marnen Laibow-Koser Apr 19 '12 at 22:20
  • But I generally recommend against JS templates, because it's usually more maintainable to have the server do all rendering, and then just pass pre-rendered HTML to the browser. – Marnen Laibow-Koser Apr 19 '12 at 22:21
  • And could you comment how do you generate smthng like a feed loading its data using ajax? Pure html code is generated on server side? – icanhazbroccoli Apr 20 '12 at 04:16
  • Easily. The client sends an Ajax request (using static JS) for new entries in feed. The server processes the request, renders HTML, and returns the HTML to the client. The client inserts the HTML in the DOM. – Marnen Laibow-Koser Apr 20 '12 at 05:43
  • Pros of this way is that nothing to do on client. Cons are in serious network traffic overhead, this data is not applicable for api usage and. That should be noted as well I think. – icanhazbroccoli Apr 20 '12 at 06:43
  • Yes, there's a little more data, but that's not a big deal in most applications compared to the maintainability gain. I18N is nearly impossible to manage if rendering is split between client and server. – Marnen Laibow-Koser Apr 20 '12 at 14:07
  • I should note that I'm largely referring to display text. Constant UI elements can come from the client. – Marnen Laibow-Koser Apr 20 '12 at 14:09
-1

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.

Marnen Laibow-Koser
  • 5,959
  • 1
  • 28
  • 33
  • ...oh, and the `"#{}"` isn't necessary if there's nothing else in the string. – Marnen Laibow-Koser Apr 19 '12 at 20:24
  • You obviously have no idea about how rails js updates/inserts/shows work – mhenrixon Apr 23 '12 at 18:02
  • @mhenrixon I know how Rails works with JavaScript. Do you have a specific thing you disagree with here, or are you just handwaving? – Marnen Laibow-Koser Apr 23 '12 at 19:09
  • Accoding to http://stackoverflow.com/a/5032666/61249 it looks perfectly fine to me and so much cleaner than the erb. These partials based on the rails action is meant to be used meaning we SHOULD generate js/coffescript from update.js.haml/erb because it's productive and convenient and you don't have to manually connect everything that the rails team has already done for you. – mhenrixon Apr 23 '12 at 20:52
  • @mhenrixon Your comment is unclear; try explaining again what you mean (in particular, I don't see how the post you linked to is particularly relevant). But unfortunately, many of the JS architectural practices that the Rails framework encourages are truly terrible (though things have improved a bit in Rails 3). This is one area where the Rails way is not always the right way. – Marnen Laibow-Koser Apr 23 '12 at 21:09