0

I've a nested objects like:

Album
-- Track 1
----Clip
--Track 2
----(None)
Album
--Track x
----Clip

i've created viewModel using knockout mapping plugin. Everything is working as expected (in fact with help of this thread can't resolve knockout js with nested objects)

Now the problem is Clip object for Track 2 does not exist initially. In the interface, user created it and i got proper JSON from server (ajax call) about this clip object. Now, how can I update my viewModel (viewModel.albums is the mapped object) to push this Clip info to Track 2 from my Ajax callback? I do not have any clue about the context of the Track from the ajax callback. If I had, it would be easier, I think.

If the question does not make sense, I will try to put something more visual in jsfiddle. Thanks in advance.

Community
  • 1
  • 1
HungryCoder
  • 7,506
  • 1
  • 38
  • 51

1 Answers1

0

It sounds like you want something like this:

viewModel.albums()[i].tracks()[j].clip = ko.mapping.fromJS(data);

In which i is which album you want to push it to and j is which track in that album you want to push it to.

Josh Schultz
  • 8,000
  • 9
  • 32
  • 39
  • thanks but it is not possible as i do not have album/track data in this context. i got a solution using `contextFor/dataFor` but problem is that DOM is not updating properly though the new values are reflected nicely in the viewmodel. I stored the element in a contextElement variable. Then `track = ko.dataFor(contextElement) track.clip = ko.mapping.fromJS(data)`! `obj_viewmodel.albums()[1].tracks()[1].clip` gives me correct value but not reflected in dom – HungryCoder Sep 24 '12 at 17:40