i'm trying to learn knockoutjs and can't figure out how the children observable works! i've tried reading many articles and many problems even on SO but probably missing something somewhere.
my json structure is (generated by Rails json renderer):
Albums -> Tracks -> Metadata
Album
-Track
--Metadata
-Track
--Metadata
Album
-Track
-- Metadata
etc.
My view's are nested as well. In a page, each album goes under a div and each track goes under table row in the above div.
My View and JS codes are in JS Fiddle. http://jsfiddle.net/var56/
Problem: If you see the view in JSFiddle, you will see that i've marked html part which is working (album) and which is not working (track). The initial view rendered correctly. When I update the album name in the text field, the above h2 is updating correctly. but in the track template, when i update the track title, it is not updating in the underneath span.
Moreover, i've tried to export the data in the chrome console.
ko.toJSON(viewModel)
or, ko.mapping.toJSON(viewModel)
it gives me old data (that means, the changed values are not returned).
I will also add/remove albums & tracks and want the viewModel and views to reflect the same.
Where i'm missing something?
Note: Actual JSON data is here:
[
{ "id":9,
"image_path":null,
"name":"Test Album",
"price":null,
"sort_order":null,
"tracks":[
{ "file_name":"01._Ei_Mon_Joshonay.mp3",
"file_path":"/media/tracks/1/01._Ei_Mon_Joshonay.mp3",
"id":192,
"length":null,
"metadata":{
"artist":"Aroti (MR present)",
"composer":"",
"created_at":"2012-09-10T11:33:42Z",
"duration":211,
"genre":"Other",
"id":124,
"lyrics":null,
"title":"01. Ei Mon Joshonay.mp3",
"track_id":192,
"updated_at":"2012-09-10T11:33:54Z",
"year":0
},
"price":null,
"thumb":"/media/track_thumbs/1/thumb_192.jpg",
"title":"01. Ei Mon Joshonay.mp3"
},
{ "file_name":"03._Jare_Ure_Ja_Pakhi.mp3",
"file_path":"/media/tracks/1/03._Jare_Ure_Ja_Pakhi.mp3",
"id":193,
"length":null,
"metadata":{
"artist":"MR present",
"composer":"",
"created_at":"2012-09-10T11:33:48Z",
"duration":204,
"genre":"lata",
"id":125,
"lyrics":null,
"title":"03.Jare ure ja pakhi",
"track_id":193,
"updated_at":"2012-09-10T11:33:54Z",
"year":0
},
"price":99.0,
"thumb":null,
"title":"03.Jare ure ja pakhi"
}
]
},
{ "id":11,
"image_path":null,
"name":"Album 2",
"price":null,
"sort_order":null,
"tracks":[ ]
}
]
UPDATE:
i could not get anything working. now i've tried creating a jsFiddle that can simulate the problem i'm facing.
please check http://jsfiddle.net/Bb538/3/.
I've now a mapping object too (though in the code you may see that is not referenced, you can try if you want). You will see that album names are updating correctly. But Title and File Name fields are not updating the adjacent span (bound to same observable).