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 [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---