我有一个包含元素的设置页面。每个元素都绑定到ViewModel属性。
我正在尝试实施编辑和保存设置。
function saveSettings() {
var new_settings = {};
new_settings.property1 = settingsViewModel.settings.property1();
new_settings.property2 = settingsViewModel.settings.property2();
new_settings.property3 = settingsViewModel.settings.property3();
.........
}
如果内容没有被修改 - 那么效果很好。但对于内容被用户更改的输入,我得到一个错误“对象的属性xxx不是函数”。看起来像我的Viewmodel和绑定在元素中发生变化后被破坏。
我的绑定如下:
<input data-bind="value: settings.property1, valueUpdate: 'afterkeydown'" />
我也试过'keyup','keypress','input' - 没什么用。
有什么想法吗?
答案 0 :(得分:0)
如果您只想将视图模型转换为JSON以发送到服务器,可以在其上使用ko.toJSON,或者如果您需要更多控制,请使用映射插件执行相同的操作?