Skip to content

Instantly share code, notes, and snippets.

@minichate
Forked from thurloat/longer.js
Created February 4, 2011 02:39
Show Gist options
  • Save minichate/810654 to your computer and use it in GitHub Desktop.
Save minichate/810654 to your computer and use it in GitHub Desktop.
/*
* Long and Dreary.
*/
var json = xml2json.parser(response_text),
query_result = json.envelope.body.queryresponse.result,
users = [];
// Build up a list of {{sd.User}}s to return to the view.
if(query_result.size == 1){
// Only a single user returned from the search
users.push(new sd.User(query_result.records));
} else if(query_result.size > 0){
for (var query_i=0; query_i < query_result.records.length; query_i++) {
users.push(new sd.User(query_result.records[query_i]));
};
}
return users
/*
* Short and Sweet.
*/
var json = xml2json.parser(response_text),
query_result = json.envelope.body.queryresponse.result,
records = query_result.records.length ? query_result.records : [query_result.records],
users = [];
// Build up a list of {{sd.User}}s to return to the view.
for (record in records) {
users.push(new sd.User(record));
};
return users;
@thurloat
Copy link

thurloat commented Feb 4, 2011

The ternary operator works here, since it's a straight object or a collection of objects in my case. It wouldn't work, however if it was only a string. (I do have cases of that [a collection of strings or a single string]) in my project right now, but I think ternary is the way to go in this case. Thx!

Also, I can't incorporate the for ( x in y ) iterator, because I've locked myself into an Array prototype extension, and it would Iterate over that function after the indexes of the array.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment