[英]Model Delete Record Ember
I have reproduced in this example my problem, i have created the actions to add and remove transactions in an invoice, the problem is that in my Ember-data the transaction removed is not deleted in the record. 在此示例中,我重现了我的问题,我创建了添加和删除发票中交易的操作,问题是在我的Ember数据中,删除的交易未在记录中删除。
this the code where i am doing something not correct 这是我在做不正确的代码
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>
Although i am able to remove the transaction object, when i debug i can still see the transaction in the record data 尽管我可以删除事务对象,但是在调试时我仍然可以在记录数据中看到事务
The transaction to be deleted is the one checked 要删除的事务是已检查的事务
I attach the images showing the issue: 我附上显示问题的图像:
Before Delete 删除之前
After Delete 删除后
As you can see the num transactions is still 3 after removing it 如您所见,删除后num交易仍然为3
What i am doing wrong in deleting the record? 我在删除记录时做错了什么?
You probably need to iterate over the array and delete each record individually: 您可能需要遍历数组并分别删除每个记录:
remove: function() {
var allSelectedItems = this.get("model.transactions").filterBy("isChecked", true);
this.get('model.transactions').removeObjects(allSelectedItems)
allSelectedItems.forEach(function(item) {
item.deleteRecord();
});
}
Also, actions never return anything so no need for the return
. 另外,动作永远不会返回任何东西,因此不需要
return
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.