如果更改一个对象中的属性,则另一个对象中的相同属性将在同一observableArray
中进行更新。
从用户界面:如果我在每个手风琴上单击“更改地址”按钮,则它正在调用populateSelectedAppoinment
函数,在此过程中,我可以在currentData
中获取当前更新的数据。
当我将currentData
中可观察到的值分配给observableArray
的第一个对象时,在相同的observableArray
上进行迭代时,第二个对象中的相同属性被修改为相同的值。哪个不应该发生。
{
populateSelectedAppoinment: function(currentData) {
var newShipmentDeliveryAddr = this.viewModel.observables.shipmentAddrArray();
var currentOrderAction = sessionStorage.getItem('currentOrderAction');
this.viewModel.observables.shipmentAddrArray([]);
var that = this;
_.each(newShipmentDeliveryAddr, function (shipAddr) {
if (shipAddr && shipAddr.serviceIDX9 && (shipAddr.serviceIDX9 === currentData.serviceIDX9)) {
shipAddr.selectedAppoinmentX9 = currentData.selectedSlotValue;
shipAddr.deliveryInstructionsX9 = currentData.deliveryInstructions;
}
if (shipAddr && shipAddr.serviceIDX9 && currentOrderAction && (shipAddr.serviceIDX9 === currentOrderAction)) {
shipAddr.shipingDetailsX9.customerAddressAddressBuildingValue = currentData.customerAddressAddressBuildingValue() === undefined ? '' : currentData.customerAddressAddressBuildingValue();
shipAddr.shipingDetailsX9.customerAddressBuildingClassValue = currentData.customerAddressBuildingClassValue() === undefined ? '' : currentData.customerAddressBuildingClassValue();
var customerAddressBuildingValue = shipAddr.shipingDetailsX9.customerAddressAddressBuildingValue;
var customerAddressBuildingClassValue = shipAddr.shipingDetailsX9.customerAddressBuildingClassValue;
shipAddr.shipingDetailsX9.deliveryAddressX9 = customerAddressBuildingValue + customerAddressBuildingClassValue;
sessionStorage.setItem('currentOrderAction', null);
}
that.viewModel.observables.shipmentAddrArray.push(shipAddr);
});
}
}
EX:
当我对对象0进行更改并在整个observableArray
上完成迭代之后,我期望得到以下结果,
0:serviceID:'1234',
shipmentDetails:{customerAddressBuildingValue: "ABC",
customerAddressBuildingClassValue: "123"}
1:serviceID:'6789',
shipmentDetails:{customerAddressBuildingValue: "XYZ",
customerAddressBuildingClassValue: "567"}
但是我得到了:
0:serviceID:'1234',
shipmentDetails:{customerAddressBuildingValue: "ABC",
customerAddressBuildingClassValue: "123"}
1:serviceID:'6789',
shipmentDetails:{customerAddressBuildingValue: "ABC",
customerAddressBuildingClassValue: "123"}