[英]Model Delete Record Ember
在此示例中,我重現了我的問題,我創建了添加和刪除發票中交易的操作,問題是在我的Ember數據中,刪除的交易未在記錄中刪除。
這是我在做不正確的代碼
actions: {
add: function() {
var transactionRecord = this.store.createRecord('transaction', {
name: 'new transaction',
isChecked: false
});
return this.get("model.transactions").addObject(transactionRecord);
},
remove: function() {
var allSelectedItems = this.get("model.transactions").filterBy("isChecked", true);
return this.get('model.transactions').removeObjects(allSelectedItems).deleteRecord(transactionRecord);
},
}
<td><button {{action "add"}}>Add New Transaction</button>
<button {{action "remove"}}>Remove Transaction</button></td>
盡管我可以刪除事務對象,但是在調試時我仍然可以在記錄數據中看到事務
要刪除的事務是已檢查的事務
我附上顯示問題的圖像:
刪除之前
刪除后
如您所見,刪除后num交易仍然為3
我在刪除記錄時做錯了什么?
您可能需要遍歷數組並分別刪除每個記錄:
remove: function() {
var allSelectedItems = this.get("model.transactions").filterBy("isChecked", true);
this.get('model.transactions').removeObjects(allSelectedItems)
allSelectedItems.forEach(function(item) {
item.deleteRecord();
});
}
另外,動作永遠不會返回任何東西,因此不需要return
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.