Rails - escape_javascript without all the \n\n\n\n\n

Hello, I'm using escape_javascript to return a partial to the browser
via ajax.

Something like:

    $("#inject").html("<%=escape_javascript(render :partial =>"feed/
index")%>");

Problem is escape_javascript ends up outputing all kinds of wasted
space like

    \n\n\n\n\n \n

Is there anyway in Rails to escape_javascript more efficiently? No
need for \n or long gaps of spaces?

Thanks

Hello, I'm using escape_javascript to return a partial to the browser
via ajax.

Something like:

$\(&quot;\#inject&quot;\)\.html\(&quot;&lt;%=escape\_javascript\(render :partial =&gt;&quot;feed/

index")%>");

Problem is escape_javascript ends up outputing all kinds of wasted
space like

\\n\\n\\n\\n\\n                \\n

Is there anyway in Rails to escape_javascript more efficiently? No
need for \n or long gaps of spaces?

I think you should be looking at your template rather than
escape_javascript - escape_javascript is just faithfully reproducing
what has been given to it. You can use -%> instead of %> in some
constructs to avoid blank lines

Fred

CuriousNewbie wrote in post #969870:

Hello, I'm using escape_javascript to return a partial to the browser
via ajax.

Something like:

    $("#inject").html("<%=escape_javascript(render :partial =>"feed/
index")%>");

Don't do that. Keep your JavaScript files static. ERb in JavaScript is
a bad idea.

In this case, put the HTML in a hidden div with a known ID, then have
the JS read that div.

Best,

I happen to disagree completely, dynamic javascript is very helpful. I have never had the whitespace issue, however, so I would perhaps look to editing your partial.

Please quote when replying.

Garrett Lancaster wrote in post #969887:

I happen to disagree completely, dynamic javascript is very helpful.

Yes -- if your overall design is flawed. I've done complex Ajax
development (not with Rails, though) and never needed dynamic JS. I
maintain that dynamic JS is a design smell, and that the overall design
should be corrected if dynamic JS starts to look like the appropriate
solution.

Under what circumstances do you think dynamic JS is appropriate?

I
have never had the whitespace issue, however, so I would perhaps look to
editing your partial.

Indeed. Or even a JS minifier.

Best,

Please quote when replying.

Garrett Lancaster wrote in post #969887:

I happen to disagree completely, dynamic javascript is very helpful.

Yes -- if your overall design is flawed. I've done complex Ajax
development (not with Rails, though) and never needed dynamic JS. I
maintain that dynamic JS is a design smell, and that the overall design
should be corrected if dynamic JS starts to look like the appropriate
solution.

What do you exactly mean by dynamic... Can you provide some examples
in which having dynamic JS can lead to design/development issues?

Would you classify JSON as dynamic? In some way, json is just
like javascript variables.

Also, what do you mean by hidden div? Do you mean you just jam
the data in it or use it for html templating?

Please quote when replying.

Matteo Latini wrote in post #969994:

What do you exactly mean by dynamic...

By "dynamic JavaScript", I mean JavaScript generated on the fly with RJS
or ERb rather than an unchanging static .js file.

Can you provide some examples
in which having dynamic JS can lead to design/development issues?

I think the use of dynamic JS complicates things needlessly. It also
prevents the client from caching the JS file. In general, source code
(behavior) should be static and data should be dynamic. This is true on
the server side and I believe it's also true on the client side.

Dynamic code is also harder to test and debug.

Would you classify JSON as dynamic? In some way, json is just
like javascript variables.

I would classify JSON as data. Since data can be dynamic, I think it is
entirely appropriate for an Ajax request (or whatever) to return dynamic
JSON, HTML, XML, CSV, or whatever. Usually this would then be processed
by a static JavaScript file.

Again, my principle is: dynamic data, static source code/behavior.
Rails' dynamic JS crams the data in with the source code, where it
really doesn't belong. You wouldn't do that in Ruby; don't do it in JS.

Also, what do you mean by hidden div?

A div with display: none in its style.

Do you mean you just jam
the data in it or use it for html templating?

I don't understand what you're asking.

Best,

Please quote when replying.

Matteo Latini wrote in post #969994:

What do you exactly mean by dynamic...

By "dynamic JavaScript", I mean JavaScript generated on the fly with RJS
or ERb rather than an unchanging static .js file.

Can you provide some examples
in which having dynamic JS can lead to design/development issues?

I think the use of dynamic JS complicates things needlessly. It also
prevents the client from caching the JS file. In general, source code
(behavior) should be static and data should be dynamic. This is true on
the server side and I believe it's also true on the client side.

Dynamic code is also harder to test and debug.

Would you classify JSON as dynamic? In some way, json is just
like javascript variables.

I would classify JSON as data. Since data can be dynamic, I think it is
entirely appropriate for an Ajax request (or whatever) to return dynamic
JSON, HTML, XML, CSV, or whatever. Usually this would then be processed
by a static JavaScript file.

Again, my principle is: dynamic data, static source code/behavior.
Rails' dynamic JS crams the data in with the source code, where it
really doesn't belong. You wouldn't do that in Ruby; don't do it in JS.

Ruby is actually NOT like that... Actually everything we love about rails
is thanks to dynamic code...

Also, what do you mean by hidden div?

A div with display: none in its style.

Do you mean you just jam
the data in it or use it for html templating?

I don't understand what you're asking.

Do you use html/js templating, or the hidden div is filled with
data?

Matteo Latini wrote in post #970246:

really doesn't belong. You wouldn't do that in Ruby; don't do it in JS.

Ruby is actually NOT like that... Actually everything we love about
rails
is thanks to dynamic code...

That's not true at all. Most of it is due to metaprogramming, which is
a very different thing than dynamically generating source code as text
-- in fact, the point of Ruby's metaprogramming is that you *don't* have
to dynamically generate source code to have dynamic behavior. Please
get your facts straight.

(There are a few spots deep in the Rails framework where source code is
generated as strings, presumably for efficiency reasons. I regard these
as either errors or performance hacks, and I would never attempt to do
likewise in my own application code.)

Also, what do you mean by hidden div?

A div with display: none in its style.

Do you mean you just jam
the data in it or use it for html templating?

I don't understand what you're asking.

Do you use html/js templating, or the hidden div is filled with
data?

That's not an "or" question, is it? Or am I still not understanding
what you mean?

Best,