I only started to use Knockout but I was already experienced with MVVM as applied in WPF. The issue I am encountering is that the my Knockout view model wrapping the model object does not update that original model. Here is a small example:
HTML
<select name="size" id="sel-size" data-bind="options: sizes, value: size"></select>
JS
// default settings
var settings = {
size: 5
};
// settings view model
var settingsVM = function () {
return {
sizes: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
size: ko.observable(settings.size)
}
}();
// subscribe to changes
settingsVM.size.subscribe(function() {
alert("Model: " + settings.size + " / VM: " + settingsVM.size());
})
ko.applyBindings(settingsVM);
When the drop-down selection is changed, only settingsVM.size()
from the view model is updated but settings.size
from the model remains the same.
It seems that initializing an observable
with a reference does not keep that reference as a backing field for the property accessor. What am I missing about the MVVM way of Knockout?