I'm trying to do the following: (see sample in jsfiddle:http://jsfiddle.net/graphicsxp/QJK99/1/)
javascript:
var originalData = {
id: 1,
name: "Main",
children: [ { id: 2, name: "bob" }, { id: 3, name: "ted" } ],
selectedChild: { id: 2, name: "bob" }
};
var viewModel = ko.mapping.fromJS(originalData);
viewModel.selectChild = function(){
var obj = { id: 9, name: "new" };
viewModel.selectedChild(obj);
}
ko.applyBindings(viewModel);
HTML:
<button data-bind="click: selectChild">click me</button>
<br/>
<span data-bind="text: selectedChild.name"></span>
but that does not display the selectedChild property. What am I doing wrong ?
[EDIT]
I had to add parenthesis to this code:
<span data-bind="text: selectedChild().name"></span>
which is fine, but I also had to add this line of code after mapping:
viewModel.selectedChild = ko.observable(viewModel.children()[0]);
which is super annoying. Why do I need to implicitely set the property to observable ? Plus, if I don't give it a default value, it won't work. WHy ?