My current solution is not as pretty as I''d like... the need for this came up when I wanted to pass a url from a controller to a javascript function. I ended up putting the URL in the id of a blank placeholder div giving it a unique class name (i.e. class="my_controller_url"), then using the CSS Behaviour class (http://www.ripcord.co.nz/behaviour/) to run a javascript function that gets the id of the div and sets the appropriate class variables. Is there any best practice used to do this? ___________________ Ben Jackson Diretor de Desenvolvimento ben-p14LI7ZcAE/pVLaUnt/cCQC/G2K4zDHf@public.gmane.org http://www.incomumdesign.com
On Saturday 09 July 2005 21:59, Ben Jackson wrote:> Is there any best practice used to do this?It depends mostly on what you want to achieve in the end. Obviously you have to put something into the page, thus why don''t you just put a script-element there where the function is called with the appropriate arguments? If you absolutely want to avoid JavaScript code in your page then you''ll have to pay the price, i.e., you''ll get some other ugliness. You could do it like this <div id="arguments_for_myfunction" style="display:none"> <span>first argument</span> <span>second argument</span> </div> myfunction.apply(window, $(''arguments_for_myfunction'').getElementsByTagName(''span'')); Michael -- Michael Schuerig Face reality and stare it down mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org --Jethro Tull, Silver River Turning http://www.schuerig.de/michael/
David Heinemeier Hansson
2005-Jul-10 11:31 UTC
Re: Re: Sending data from controller to Javascript
> > Is there any best practice used to do this? > > It depends mostly on what you want to achieve in the end. Obviously you > have to put something into the page, thus why don''t you just put a > script-element there where the function is called with the appropriate > arguments?That''s what we do. I basically have something like: <%= javascript_tag "var listUpdateUrl = ''#{url_for(...)}''" %> Best practice in Rails is to use the Javascript helpers. Which unfortunately doesn''t gel too well with the Behavior approach. For now, that''s considered a more than acceptable loss considering what you get in return (ease of use, speed of development, less manual js). -- David Heinemeier Hansson http://www.loudthinking.com -- Broadcasting Brain http://www.basecamphq.com -- Online project management http://www.backpackit.com -- Personal information manager http://www.rubyonrails.com -- Web-application framework
Just to clarify... the reason we started doing things this way was that we could not figure out how else to evaluate JS that was being inserted in the page. Is there a standard way of calling js functions after an AJAX request? - B On Jul 10, 2005, at 8:31 AM, David Heinemeier Hansson wrote:>>> Is there any best practice used to do this? >> >> It depends mostly on what you want to achieve in the end. Obviously >> you >> have to put something into the page, thus why don''t you just put a >> script-element there where the function is called with the appropriate >> arguments? > > That''s what we do. I basically have something like: > > <%= javascript_tag "var listUpdateUrl = ''#{url_for(...)}''" %> > > Best practice in Rails is to use the Javascript helpers. Which > unfortunately doesn''t gel too well with the Behavior approach. For > now, that''s considered a more than acceptable loss considering what > you get in return (ease of use, speed of development, less manual js). > -- > David Heinemeier Hansson > http://www.loudthinking.com -- Broadcasting Brain > http://www.basecamphq.com -- Online project management > http://www.backpackit.com -- Personal information manager > http://www.rubyonrails.com -- Web-application framework > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
On Sunday 10 July 2005 21:16, Ben Jackson wrote:> Just to clarify... the reason we started doing things this way was > that we could not figure out how else to evaluate JS that was being > inserted in the page. Is there a standard way of calling js functions > after an AJAX request?If you''re using Rails 0.13, have a look at prototype.js. AjaxUpdater has a new option, evalScripts, that causes it to evaluate scripts contained in the ajax response. Michael -- Michael Schuerig All good people read good books mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org Now your conscience is clear http://www.schuerig.de/michael/ --Tanita Tikaram, Twist In My Sobriety
Note that the JavaScript helpers for AJAX calls turn this option on by default. Thomas Am 10.07.2005 um 22:02 schrieb Michael Schuerig:> On Sunday 10 July 2005 21:16, Ben Jackson wrote: > >> Just to clarify... the reason we started doing things this way was >> that we could not figure out how else to evaluate JS that was being >> inserted in the page. Is there a standard way of calling js functions >> after an AJAX request? >> > > If you''re using Rails 0.13, have a look at prototype.js. > AjaxUpdater has > a new option, evalScripts, that causes it to evaluate scripts > contained > in the ajax response. > > Michael > > -- > Michael Schuerig All good people read good books > mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org Now your conscience is clear > http://www.schuerig.de/michael/ --Tanita Tikaram, Twist In My Sobriety > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >