Alertify 确认不适用于 Ember js

Alertify confirm does not work with Ember js

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

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 不起作用。我试过将商店作为服务注入,但这也不起作用。有没有办法让这个确认框起作用?

您在函数中使用了 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!');
    }
  });
}