Pergunta

I have a REST service that returns a collection that contains non- normalized data. For example, it might look something like this:

root
.
root:CollectionItem[0].alpha
root:CollectionItem[0].beta
root:CollectionItem[0].zappa
.
root:CollectionItem[1].alpha
root:CollectionItem[1].children[0].MoonUnit
root:CollectionItem[1].children[1].Dweezil
root:CollectionItem[1].zappa
.
root:CollectionItem[2].alpha
root:CollectionItem[2].beta
root:CollectionItem[2].zappa

What's the best strategy for using the Mapping PlugIn with non-normalized JSON data like this? The problem is that the PlugIn seems to use CollectionItem[0] as a template, but when I foreach through a result, Knockout is blowing up when I try to read root.CollectionItem[1].beta (which doesn't exist).

EDIT #1: I guess Knockout isn't as popular as I thought... This question seems destined for a Tumbleweed Award. :-(

EDIT #2: Got a workable answer on the Knockout Forum http://groups.google.com/group/knockoutjs/browse_thread/thread/6705c222481c7b75#, so I don't have to worry about another Tumbleweed Award! :-)

Foi útil?

Solução

I got an answer from someone named NTB on the Knockout JS forum. Here's his solution on JS fiddle in case anyone is interested. Hate to answer my own question, but it is what it is.

http://jsfiddle.net/PxeyY/

Here's his summary:

"I have sometimes manually "massaged" the data before calling the mapper, or created a dummy shell object that has all the possible properties set to null (or [] in the case of arrays) -- then use $.extend(dummy, real) to create something .fromJS() can work with. You'd have to iterate through the array with that pattern, but that's my $.02 anyway.

"Would like to hear cleaner solutions myself too though."

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top