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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2010-Dec-21 20:28 UTC
Re: Rails - escape_javascript without all the \n\n\n\n\n
On Dec 21, 8:19 pm, CuriousNewbie <bhellm...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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? >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> Thanks-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Marnen Laibow-Koser
2010-Dec-21 20:46 UTC
Re: Rails - escape_javascript without all the \n\n\n\n\n
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, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org Sent from my iPhone -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Garrett Lancaster
2010-Dec-21 20:57 UTC
Re: Rails - escape_javascript without all the \n\n\n\n\n
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. On Dec 21, 2010, at 2:46 PM, Marnen Laibow-Koser wrote:> 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, > -- > Marnen Laibow-Koser > http://www.marnen.org > marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > > Sent from my iPhone > > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Marnen Laibow-Koser
2010-Dec-21 21:03 UTC
Re: Rails - escape_javascript without all the \n\n\n\n\n
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, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Matteo Latini
2010-Dec-22 08:15 UTC
Re: Re: Rails - escape_javascript without all the \n\n\n\n\n
On Dec 21, 2010, at 10:03 PM, Marnen Laibow-Koser wrote:> 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?> 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, > -- > Marnen Laibow-Koser > http://www.marnen.org > marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Marnen Laibow-Koser
2010-Dec-22 17:05 UTC
Dynamic JavaScript -- was: Re: Re: Rails - escape_javascript without all the \n\n\n\n\n
Please quote when replying. Matteo Latini wrote in post #969994:> On Dec 21, 2010, at 10:03 PM, Marnen Laibow-Koser wrote: > >> > 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, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org Sent from my iPhone -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Matteo Latini
2010-Dec-23 08:51 UTC
Re: Dynamic JavaScript -- was: Re: Re: Rails - escape_javascript without all the \n\n\n\n\n
On Dec 22, 2010, at 6:05 PM, Marnen Laibow-Koser wrote:> Please quote when replying. > > Matteo Latini wrote in post #969994: >> On Dec 21, 2010, at 10:03 PM, Marnen Laibow-Koser wrote: >> >>> >> 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?> > Best, > -- > Marnen Laibow-Koser > http://www.marnen.org > marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > > Sent from my iPhone > > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Marnen Laibow-Koser
2010-Dec-23 15:56 UTC
Re: Dynamic JavaScript -- was: Re: Re: Rails - escape_javascript without all the \n\n\n\n\n
Matteo Latini wrote in post #970246:> On Dec 22, 2010, at 6:05 PM, Marnen Laibow-Koser wrote: > >> >>> >> 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, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org Sent from my iPhone -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.