【发布时间】:2017-09-29 17:19:54
【问题描述】:
例如我有以下模型:
var Volume = function (id, path, isactive) {
var self = this;
self.id = ko.observable(id);
self.path = ko.observable(path);
self.isactive = ko.observable(isactive);
self.Save = function (data) {
//ajax post
}
self.Update = function (data) {
//ajax post
}
}
var ViewModel = function (data) {
var self = this;
self.volumes = ko.observableArray(data.volumes.map(function (item) {
return new Volume(item.id, item.path, item.isActive, item.description);
}));
self.AddVolume = function () {
self.volumes.push(new Volume());
}
}
保存或更新后,我想从 Volume 模型中刷新父 ViewModel,因为数据库中的某些值已更改。
如何重新初始化 ViewModel?
var viewModel = new ViewModel(ko.utils.parseJson(data) || []);
ko.applyBindings(viewModel);
【问题讨论】:
-
刷新父 ViewModel 是什么意思?
-
将新数据绑定到 ViewModel
-
您是否在
Save和Updateajax 回调中更新您当前的Volume? -
当前卷和阵列中的其他卷也受到影响,这就是我要刷新屏幕的原因。
-
如果没有完整的代码,我想说您可以在创建时将您的
ViewModel提供给您的Volume,并在其上调用Refresh方法。
标签: javascript mvvm knockout.js