1

我正在尝试制作一个弹出框来确认您是否要删除文档。如果我尝试:

if(alertify.confirm("Delete this?')) {
    this.store.findRecord('document', docId, {backgroundReload: false}).then((doc) => {
    doc.destroyRecord();
    alertify.success('Document successfully deleted!');
}

它不会在运行删除代码之前等待确认,因为据我了解, alertify.confirm 是非阻塞的。如果我尝试:

deleteFile(docId) {
  alertify.confirm("Are you sure you want to delete this document?", function (e) {
    if (e) {
      this.store.findRecord('document', docId, {backgroundReload: false}).then((doc) => {
        doc.destroyRecord();
        alertify.success('Document successfully deleted!');
      });
    } else {
      alertify.error('Something went wrong!');
    }
  });
}

它确实要求确认,但删除代码不起作用,因为商店未定义,所以 findRecord 不起作用。我尝试将商店作为服务注入,但这也不起作用。有没有办法让这个确认框工作?

4

1 回答 1

2

this在函数中使用,因此指的是该函数的 this 上下文。您可以使用胖箭头函数或将外部 this 分配给变量。前者看起来像这样:

deleteFile(docId) {
  alertify.confirm("Are you sure you want to delete this document?", (e) => {
    if (e) {
      this.store.findRecord('document', docId, {backgroundReload: false}).then((doc) => {
        doc.destroyRecord();
        alertify.success('Document successfully deleted!');
      });
    } else {
      alertify.error('Something went wrong!');
    }
  });
}
于 2017-06-19T12:45:08.127 回答