我在_initializeData中有一个新创建的json数据模型。目的是当用户尝试删除购物车中的物品时,我想将该物品存储在此新数据模型中。因此,在单击删除按钮时,我正在读取具有购物车中所有项目的json数据模型,然后使用已删除元素的索引将结果复制到新模型中。如果我仅删除一项,它会正常工作。但是,如果我在另一个项目上按“删除”,则新模型将覆盖新模型中的上一个条目。它不像数组那样保存所有条目。
_initializeData: function () {
var removedData = {}; // new model for removed parts
removedData.removedParts = [];
this.removedItems = new JSONModel(removedData);
this.getView().setModel(this.removedItems, "removedItems");
}
// button click
onRemovePart: function (oEvent) {
var idx = this.getView().byId("table").getSelectedIndex();
// retrieveing the model which has all the parts from the table
var data = this.getView().getModel("materialData").getProperty("/partsData");
// reading the removed item data
var removedPart = data[idx];
// assigning it to the new model. But it does not hold multiple
entries rather over-writes the previous one
this.getView().getModel("removedItems").setProperty("/removedParts", removedPart);
答案 0 :(得分:2)
使用inline
避免引用。在这里,引用引起了压倒性的行为
答案 1 :(得分:1)
问题在于您要覆盖模型上的完整对象:removeItems。
由于您将其覆盖,所以旧数据将丢失。因此,您所需要做的就是获取已删除的旧项目数组,将元素推入其中,然后将其设置为模型。
_initializeData: function () {
var removedData = {}; // new model for removed parts
removedData.removedParts = [];
this.removedItems = new JSONModel(removedData);
this.getView().setModel(this.removedItems, "removedItems");
}
// button click
onRemovePart: function (oEvent) {
var idx = this.getView().byId("table").getSelectedIndex();
// retrieveing the model which has all the parts from the table
var data = this.getView().getModel("materialData").getProperty("/partsData");
// reading the removed item data
var removedPart = data[idx];
// assigning it to the new model. But it does not hold multiple
entries rather over-writes the previous one
// Get the old deleted items first :
var oDeletedItems = this.getView().getModel("removedItems").getProperty("/removedParts");
oDeletedItems.removedParts.push(removedPart); // the array is removedParts and oDeletedItems is the obejct which stores the array as per _initlaise method
this.getView().getModel("removedItems").setProperty("/removedParts", oDeletedItems);
希望这会有所帮助。