-
-
Save minichate/810654 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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; |
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
Not sure if my ternary shortcut is actually valid in this case -- depends on that actual types I think? in any case, this is better 'cause its worst case O(n) instead of O(2n) -- and the compiler can optimize the shit out of it so it'll prolly execute in O(1)