Alertify确认不适用于Ember js

时间:2017-06-19 12:17:40

标签: javascript ember.js alertify alertifyjs

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

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

1 个答案:

答案 0 :(得分:2)

您在函数中使用this,因此引用该函数的this-context。您可以使用胖箭头函数或将外部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!');
    }
  });
}