The excellent answer by Paz got me through a rough patch this week with the autocomplete jQuery UI deal.
All that to say, How does one choose the correct json response if the you have two different "base" objects in your json response.
Simply put, I have a ruby instance variable made up of two models, Pitch and User. I want to autocomplete based on this instance variable. The json response is, say
[{"user":{"id":1,"name":"Blah Hugo"}},
{"user":{"id":3,"name":"Blaz Foobar"}},
{"pitch":{"id":2,"name":"Blasters On Business Apparel"}}]
The autocomplete javascript file looks like this NOW, and it works < snippet only >:
.data( "autocomplete" )._renderItem = function( ul, item ) {
return jQuery( "<li></li>" )
.data( "item.autocomplete", item )
.append( "<a>" + (item.user ? item.user.name : item.pitch.name) + "</a>" )
.appendTo( ul );
};
Again my problem is this ternary i had to use. item[0].name
doesn't work, nor did item.item[0].name
Is there no way to get the generic 'user' or 'pitch' if you don't know the name of either?
I'd have though one of them would. What says you?