Forum: Ruby on Rails best way to return JSON?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Daniel H. (Guest)
on 2006-05-24 21:58
(Received via mailing list)
I'm using Ajax.Updater to update a select element, and have just
discovered
that IE doesn't like having its innerHTML set. I figure instead of a
bunch
of option tags I can return JSON and parse it to add the options, but
I'm
not quite sure of the best way to do this.... or any way to do it really
:)
Could someone point this out to me?  Or am I using a totally antiquated
technique and should just use RJS?

Thanks
Daniel
Gardiner Allen (Guest)
on 2006-05-25 01:52
Daniel H. wrote:
> I'm using Ajax.Updater to update a select element, and have just
> discovered
> that IE doesn't like having its innerHTML set. I figure instead of a
> bunch
> of option tags I can return JSON and parse it to add the options, but
> I'm
> not quite sure of the best way to do this.... or any way to do it really
> :)
> Could someone point this out to me?  Or am I using a totally antiquated
> technique and should just use RJS?
>
> Thanks
> Daniel

I ran into the same issue of IE not liking replacements of a select
tag's innerHTML the other week. The solution I used was just to wrap the
select tag in a block, and replace the entire select tag / the block's
innerHTML. This is non-optimal, but it was quicker than producing some
JSON and having a javacript event render new select options.
Daniel H. (Guest)
on 2006-05-25 04:41
(Received via mailing list)
Yeah, I had read that technique but decided it'd be good to get some
experience messing around with JSON, just because.  Also because using
JSON
meant rewriting less code.  Here's a JS function I ended up writing, in
case
it helps:

function populateSelectFromJson(select, json)
{
	select.innerHTML = ''
	for (i = 0; i < json.length; i++)
	{
		pair = json[i]
		select.options[i] = new Option(pair[1], pair[0]);
	}
}
Phillip H. (Guest)
on 2006-05-25 04:50
(Received via mailing list)
> function populateSelectFromJson(select, json)
> {
>         select.innerHTML = ''
>         for (i = 0; i < json.length; i++)
>         {
>                 pair = json[i]
>                 select.options[i] = new Option(pair[1], pair[0]);
>         }
> }

Wouldn't:

function populateFromJSON(select, json) {
    select.options.length = 0;
    for (var i = 0; i < json.length; i++) {
        select.options[select.options.length] = new Option(json[i][1],
json[i][0]);
    }
}

be better? It avoids the innerHTML completely, and leaves space to add
a 'title' option. Some managers like that sort of thing ;)

--
Phillip H.
http://www.sitharus.com/
Daniel H. (Guest)
on 2006-05-25 04:59
(Received via mailing list)
I wasn't aware you could do that :)

I noticed you also got rid of the temp variable "pair" - how come? I
notice
some people prefer to use a temp variable and some people don't, but I'm
not
really sure why.  Hmm in my case the variable doesn't really even add
any
extra clarity.  In fact "json" isn't named well either, since it's the
result of an eval on JSON rather than the JSON itself :P .

Thanks!
Phillip H. (Guest)
on 2006-05-25 05:05
(Received via mailing list)
> I wasn't aware you could do that :)

You can do all sorts in JavaScript, it's got quite complex over the
years.

> I noticed you also got rid of the temp variable "pair" - how come? I notice
> some people prefer to use a temp variable and some people don't, but I'm not
> really sure why.  Hmm in my case the variable doesn't really even add any
> extra clarity.  In fact "json" isn't named well either, since it's the
> result of an eval on JSON rather than the JSON itself :P .

Personal preference. I use temp variables where a resolution is
expensive (eg $()) or where I'm accessing it several times. My test is
to look at it and see if I'm saving typing or not ;)

--
Phillip H.
http://www.sitharus.com/
This topic is locked and can not be replied to.