Using unnormalized data with the knockout display plug-in

I have a REST service that returns a collection, 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 Mapping PlugIn with junk data? The problem is that PlugIn seems to be using CollectionItem [0] as a template, but when I get the result through the result, the knockout explodes when I try to read root.CollectionItem [1] .beta (which doesn't exist).

EDIT # 1 : I think Knockout is not as popular as I thought ... This question seems to exist for the 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 premium! :-)

+3


source to share


1 answer


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

http://jsfiddle.net/PxeyY/

Here is his summary:



"I have sometimes manually" massaged "the data before invoking the converter or created a dummy wrapper object that has all possible properties set to null (or [] in the case of arrays) - then use $ .extend (dummy, valid) to create that something .fromJS () might work. You'll have to iterate over the array with this pattern, but mine is $ .02 anyway.

"I would like to hear cleaner solutions as well."

+3


source







All Articles