This question relates to
selected object pattern with knockout
Thanks to the answer, I am able to set the selected item via bindings.
However, how would I go about setting it in code?
For example, I want to create a new object and set that as the selected object in the model.
Normally this would go.
1) Create Object.
2) set object defaults.
3) Model.selected_object = objectJustCreated
//model
item_selected = ko.observable();
//code
var new_item = app.createItem(defaults);
model.item_selected = new_item;
I’m trying this to switch out the observable in the model but my binding are not reflecting the changes. Will I have to loop through the values and update the selected object that way?
Any help much appreciated.
When you set the value of an observable you need to pass it as the first argument to the observable (which is a function).
So, you would want to do:
Here is a sample based on the previous question: http://jsfiddle.net/rniemeyer/ST8gt/