views:

363

answers:

3

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?

+3  A: 

Haml's really meant to generate HTML. You could, in theory, use create.js.haml, but ERB makes much more sense when you're writing Javascript.

nex3
+1  A: 

I'm almost positive that when return code via AJAX that it is returned as-is for the front-end to parse/eval. This means that it bypasses the HAML parser and is sent straight to the browser.

I could be wrong, and don't have time to test. Your quickest way to check is to simply change the file to ".haml", and if it still works, then you're good to go.

Mike Trpcic
A: 

In a js.haml it would look something like

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

Note the HAML != syntax which prevents the generated text from being HTML escaped. I do agree that ERB is a better fit here, but for consistency stake you might still want to go the HAML way.

adnans