从另一个模型更新一个模型
问题描述:
我有两个相同类型的模型,型号A和模型B。我想更新型号A,其型号为B。这两种模式都绑定到用户界面的不同区域。从另一个模型更新一个模型
有没有办法做到更新而不必去财产的财产?
// address = model A
// this.editingBillingAddress() = model B
var address = ko.utils.arrayFirst(this.userAddresses(), function (address) {
return address.addressId() == 123;
});
//address = this.editingBillingAddress(); // this does nothing. nothing in the UI updates
// this works but I must explicitly set every property
address.name(this.editingBillingAddress().name());
address.address1(this.editingBillingAddress().address1());
address.address2(this.editingBillingAddress().address2());
// .... and so on until the fingers bleed
答
找到了答案在此第二评论问题:Updating Knockout.js Observable array element value。
var address = ko.utils.arrayFirst(this.userAddresses(), function (address) {
return address.addressId() == 123;
});
this.userAddresses.replace(address, this.editingBillingAddress());
答
您可以使用映射插件先转换为JavaScript对象,然后映射到一个对象,具有可观:http://knockoutjs.com/documentation/plugins-mapping.html
var jsAddress = ko.mapping.toJS(self.address());
var address = ko.mapping.fromJS(jsAddress);
self.editingBillingAddress(address);
在此情况下不起作用。地址不是可观察的。 – TugboatCaptain 2014-10-08 01:51:46
而不是self.address()你可以使用地址。 – 2014-10-08 03:14:09