observableArray的1个对象属性的更改会更改同一observableArray中的另一个对象的相同属性

时间:2019-07-06 08:28:36

标签: knockout.js

如果更改一个对象中的属性,则另一个对象中的相同属性将在同一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"}

0 个答案:

没有答案