我有两个相同类型的型号,型号 A 和型号 B 。我想使用模型 B 的属性更新模型 A 。两种模型都绑定到UI的不同区域。
有没有办法进行更新而无需按物业去物业?
// 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
答案 0 :(得分:1)
您可以使用映射插件首先转换为javascript对象,然后映射到具有observables的对象:http://knockoutjs.com/documentation/plugins-mapping.html
var jsAddress = ko.mapping.toJS(self.address());
var address = ko.mapping.fromJS(jsAddress);
self.editingBillingAddress(address);
答案 1 :(得分:0)
在此问题的第二条评论中找到答案: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());