Andre
2007-Dec-12 02:33 UTC
newbie question with prototype.js - using ajax.request together with template
I''ve just started using prototype.js (v. 1.6) and would like to do the following: 1. make a simple ajax.request to a page that returns a string 2. the string should be recognizable by the response as a javascript array 3. which i would like to then run through the template.evaluate() method one at a time to write out a string So, what I have so far is this (a little dumbed down but hopefully enough to follow?): ----------------- My humble little script ----------------------------------------------------------------------- <script> var s = ''''; s+=''<table>''; s+=''<tr class=\"rh\">''; s+=''<td>Name</td>''; s+=''<td>Type</td>''; s+=''<td>Created by</td>''; s+=''<td>Related to</td>''; s+=''<td>Date</td>''; s+=''</tr>''; var templ = new Template(''<tr><td>#{n}</td><td>#{t}</td><td>#{c}</ td><td>#{r}</td><td>#{d}</td>''); new Ajax.Request(''members/ajax.aspx?x=test'', { onSuccess: function(t){ var b = t.responseText; for (var i=0; i<b.length; i++) { s+=templ.evaluate(b[i]); } s+=''</table>''; $(''table'').innerHTML = s; } }); </script> <div id="table"></div> ----------------- /My humble little script ----------------------------------------------------------------------- The ajax response displays the following: {n:''Application Error'', t:''ProgressNoteAdded'', c:''A. Hertz'', r:''joe sample'', d:''11/22/2007''}, {n:''Application Error'', t:''ErrorInsideClass'', c:''C. Fudge'', r:''joe sample'', d:''11/22/2007''}, {n:''ProgramCreated'', t:''ProgramCreated'', c:''R. Crawford'', r:''joe sample'', d:''11/22/2007''}, {n:''Application Error'', t:''ProgressNoteAdded'', c:''A.Herz'', r:''joe sample'', d:''11/22/2007''} But the script does not work. I''ve tried many different variations and I''m sure part of it is that I have yet to fully understand what happens with the responseText and how it is evaluated. Any help would greatly be appreciated. Thanks --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Elden
2007-Dec-12 14:44 UTC
Re: newbie question with prototype.js - using ajax.request together with template
take a look on this web because you miss eval the t.responseText http://mc4j.org/confluence/display/stripes/JSON+and+Prototype and a better way to do .innerHTML $(''table'').update(s); On Dec 11, 2007 8:33 PM, Andre <arefay-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I''ve just started using prototype.js (v. 1.6) and would like to do the > following: > > 1. make a simple ajax.request to a page that returns a string > 2. the string should be recognizable by the response as a javascript > array > 3. which i would like to then run through the template.evaluate() > method one at a time to write out a string > > > So, what I have so far is this (a little dumbed down but hopefully > enough to follow?): > > ----------------- My humble little script > ----------------------------------------------------------------------- > <script> > var s = ''''; > s+=''<table>''; > s+=''<tr class=\"rh\">''; > s+=''<td>Name</td>''; > s+=''<td>Type</td>''; > s+=''<td>Created by</td>''; > s+=''<td>Related to</td>''; > s+=''<td>Date</td>''; > s+=''</tr>''; > > var templ = new Template(''<tr><td>#{n}</td><td>#{t}</td><td>#{c}</ > td><td>#{r}</td><td>#{d}</td>''); > > new Ajax.Request(''members/ajax.aspx?x=test'', { > onSuccess: function(t){ > var b = t.responseText; > > for (var i=0; i<b.length; i++) { > s+=templ.evaluate(b[i]); > } > > s+=''</table>''; > > $(''table'').innerHTML = s; > } > }); > </script> > > <div id="table"></div> > > ----------------- /My humble little script > ----------------------------------------------------------------------- > > > > The ajax response displays the following: > > {n:''Application Error'', t:''ProgressNoteAdded'', c:''A. Hertz'', r:''joe > sample'', d:''11/22/2007''}, > {n:''Application Error'', t:''ErrorInsideClass'', c:''C. Fudge'', r:''joe > sample'', d:''11/22/2007''}, > {n:''ProgramCreated'', t:''ProgramCreated'', c:''R. Crawford'', r:''joe > sample'', d:''11/22/2007''}, > {n:''Application Error'', t:''ProgressNoteAdded'', c:''A.Herz'', r:''joe > sample'', d:''11/22/2007''} > > > But the script does not work. I''ve tried many different variations and > I''m sure part of it is that I have yet to fully understand what > happens with the responseText and how it is evaluated. > > Any help would greatly be appreciated. > > Thanks > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Andre
2007-Dec-12 15:42 UTC
Re: newbie question with prototype.js - using ajax.request together with template
Hi Elden, thanks for the response. I will look at the page you suggested, but from reading the docs on prototypejs.org ( http://www.prototypejs.org/api/ajax/request ) I thought eval would happen automatically, e.g. Automatic JavaScript response evaluation Any response whose MIME type is missing or JavaScript-related will automatically be passed to eval will also change to use .update(s) rather than innerHTML Thanks again On Dec 12, 9:44 am, Elden <willy...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> take a look on this web because you miss eval the t.responseText > > http://mc4j.org/confluence/display/stripes/JSON+and+Prototype > > and a better way to do .innerHTML > > $(''table'').update(s); > > On Dec 11, 2007 8:33 PM, Andre <are...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > I''ve just started using prototype.js (v. 1.6) and would like to do the > > following: > > > 1. make a simple ajax.request to a page that returns a string > > 2. the string should be recognizable by the response as a javascript > > array > > 3. which i would like to then run through the template.evaluate() > > method one at a time to write out a string > > > So, what I have so far is this (a little dumbed down but hopefully > > enough to follow?): > > > ----------------- My humble little script > > ----------------------------------------------------------------------- > > <script> > > var s = ''''; > > s+=''<table>''; > > s+=''<tr class=\"rh\">''; > > s+=''<td>Name</td>''; > > s+=''<td>Type</td>''; > > s+=''<td>Created by</td>''; > > s+=''<td>Related to</td>''; > > s+=''<td>Date</td>''; > > s+=''</tr>''; > > > var templ = new Template(''<tr><td>#{n}</td><td>#{t}</td><td>#{c}</ > > td><td>#{r}</td><td>#{d}</td>''); > > > new Ajax.Request(''members/ajax.aspx?x=test'', { > > onSuccess: function(t){ > > var b = t.responseText; > > > for (var i=0; i<b.length; i++) { > > s+=templ.evaluate(b[i]); > > } > > > s+=''</table>''; > > > $(''table'').innerHTML = s; > > } > > }); > > </script> > > > <div id="table"></div> > > > ----------------- /My humble little script > > ----------------------------------------------------------------------- > > > The ajax response displays the following: > > > {n:''Application Error'', t:''ProgressNoteAdded'', c:''A. Hertz'', r:''joe > > sample'', d:''11/22/2007''}, > > {n:''Application Error'', t:''ErrorInsideClass'', c:''C. Fudge'', r:''joe > > sample'', d:''11/22/2007''}, > > {n:''ProgramCreated'', t:''ProgramCreated'', c:''R. Crawford'', r:''joe > > sample'', d:''11/22/2007''}, > > {n:''Application Error'', t:''ProgressNoteAdded'', c:''A.Herz'', r:''joe > > sample'', d:''11/22/2007''} > > > But the script does not work. I''ve tried many different variations and > > I''m sure part of it is that I have yet to fully understand what > > happens with the responseText and how it is evaluated. > > > Any help would greatly be appreciated. > > > Thanks- Hide quoted text - > > - Show quoted text ---~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---