首页 > 解决方案 > ember删除记录,第一次尝试后无法删除

问题描述

我有一个必须删除记录的工作流程,但如果后端抛出任何错误,我会显示一个错误对话框,上面有一个关闭按钮。用户可以关闭该错误对话框并返回主屏幕,在那里他可以再次提交删除记录的操作。但是当我第二次尝试单击删除时出现以下错误:

尝试deleteRecord在 root.deleted.inFlight 状态下处理事件

有人可以指导我潜在的问题是什么吗?

我的参考代码:

文件:component.js

let person = this.get('person') -> this is the model

person.destroyRecord().then(() => {
// if success, show success message
   this.showSuccessModel();
}, (error) => {
// show error message in a dialog, with close button
   this.showErrorModel();
});

当用户单击关闭按钮时,当他再次单击删除操作时,我收到上述错误。

标签: ember.js

解决方案


我能够解决这个问题,如果出现错误,我按照 ember 文档的建议在模型对象上使用 rollBackAttributes() 函数,但我刚刚意识到我还必须这样做:

model.send('becameInvalid'),否则只是回滚属性不起作用。因此,我的工作代码将如下所示:

let person = this.get('person') -> this is the model

person.destroyRecord().then(() => {
// if success, show success message
this.showSuccessModel();
}, (error) => {
// show error message in a dialog, with close button
   if(person.get('hasDirtyAttributes') {
     person.send('becameInvalid');
     person.rollBackAttributes();
   }
   this.showErrorModel();
});

推荐阅读