I''ve a question: I recently ran into the need to return a JSON object
from the server to the client; the properties of that object detailed
information that needed to be presented on the screen to the visitor
such that the property name is the same as the HTML Element on screen
that needs to be updated. For example, if the system gets:
{ foo: "bar", alice: "Bob" }
from the server, then I need to do:
$("foo").update("<p>bar</p>");
$("alice").update("<p>Bob</p>");
but I won''t know the names of the properties as run time.
I solved the problem by doing this:
function ajax_complete(responder, transport, json) {
if(json.use_me) {
Object.keys(json).each(function(element) {
$(element).update(eval("json."+element)); });
}
}
The ajax_complete() function is a registered responder for Ajax objects,
so it''s called all the time. Hence, the json.use_me property which
indicates when to parse the json object and put information on screen.
While the use of Object.keys() seemed to do what I needed it to, I''m
not
a fan of using eval() when I can avoid. So, is there a better way to
this sort of thing?
-- Dash --
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Two ways.. Prototype doesn''t extend Object.prototype so for.. in..
loops should work assuming you don''t have any other code present that
extends Object.prototype such as the json library (which is now
unnecessary for Prototype users as of 1.5.1).<br>
So:<br>
<br>
onSuccess: function(xhr, json){<br>
for( var key in json ){ $(key).update(json[key]);
}<br>
}<br>
<br>
Or you can use the Prototypish way which is unnecessary and inefficient
in this case:<br>
<br>
onSuccess: function(xhr,json){<br>
$H(json).each(function(pair){
$(pair.key).update(pair.value); });<br>
}<br>
<br>
Colin<br>
<br>
David Dashifen Kees wrote:
<blockquote
cite="mid:46106ABA.1010401-NT0ononE2K1Wk0Htik3J/w@public.gmane.org"
type="cite">
<meta content="text/html;charset=ISO-8859-1"
http-equiv="Content-Type">
<title></title>
I''ve a question: I recently ran into the need to return a
JSON object
from the server to the client; the properties of that object detailed
information that needed to be presented on the screen to the visitor
such that the property name is the same as the HTML Element on screen
that needs to be updated. For example, if the system gets:<br>
<br>
<blockquote>{ foo: "bar", alice: "Bob" }<br>
</blockquote>
<br>
from the server, then I need to do:<br>
<br>
<blockquote>$("foo").update("<p>bar</p>");<br>
$("alice").update("<p>Bob</p>");<br>
</blockquote>
<br>
but I won''t know the names of the properties as run time.<br>
<br>
I solved the problem by doing this:<br>
<br>
<blockquote>function ajax_complete(responder, transport, json)
{<br>
if(json.use_me) {<br>
Object.keys(json).each(function(element) {
$(element).update(eval("json."+element));
}); <br>
}<br>
}<br>
<br>
</blockquote>
The ajax_complete() function is a registered responder for Ajax
objects, so it''s called all the time. Hence, the json.use_me
property
which indicates when to parse the json object and put information on
screen. While the use of Object.keys() seemed to do what I needed it
to, I''m not a fan of using eval() when I can avoid. So, is
there a
better way to this sort of thing?<br>
<br>
-- Dash --<br>
<br>
<br>
</blockquote>
<br>
--~--~---------~--~----~------------~-------~--~----~<br>
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Spinoffs" group. <br> To post to this
group, send email to
rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org <br> To
unsubscribe from this group, send email to
rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
<br> For more options, visit this group at
http://groups.google.com/group/rubyonrails-spinoffs?hl=en <br>
-~----------~----~----~----~------~----~------~--~---<br>
</body>
</html>
<br>
Either one of those should end up working out, then. I don''t use the json library anymore since I''ve been using rc2 of 1.5.1 so that isn''t a problem. Thanks Colin! -- Dash -- Colin Mollenhour wrote:> Two ways.. Prototype doesn''t extend Object.prototype so for.. in.. > loops should work assuming you don''t have any other code present that > extends Object.prototype such as the json library (which is now > unnecessary for Prototype users as of 1.5.1). > So: > > onSuccess: function(xhr, json){ > for( var key in json ){ $(key).update(json[key]); } > } > > Or you can use the Prototypish way which is unnecessary and > inefficient in this case: > > onSuccess: function(xhr,json){ > $H(json).each(function(pair){ $(pair.key).update(pair.value); }); > } > > Colin > > David Dashifen Kees wrote: >> I''ve a question: I recently ran into the need to return a JSON >> object from the server to the client; the properties of that object >> detailed information that needed to be presented on the screen to the >> visitor such that the property name is the same as the HTML Element >> on screen that needs to be updated. For example, if the system gets: >> >> { foo: "bar", alice: "Bob" } >> >> >> from the server, then I need to do: >> >> $("foo").update("<p>bar</p>"); >> $("alice").update("<p>Bob</p>"); >> >> >> but I won''t know the names of the properties as run time. >> >> I solved the problem by doing this: >> >> function ajax_complete(responder, transport, json) { >> if(json.use_me) { >> Object.keys(json).each(function(element) { >> $(element).update(eval("json."+element)); }); >> } >> } >> >> The ajax_complete() function is a registered responder for Ajax >> objects, so it''s called all the time. Hence, the json.use_me >> property which indicates when to parse the json object and put >> information on screen. While the use of Object.keys() seemed to do >> what I needed it to, I''m not a fan of using eval() when I can avoid. >> So, is there a better way to this sort of thing? >> >> -- Dash -- >> >> > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---