0

当有人尝试从页面导航时,我正在尝试使用 alertify 确认框。但是在路由的 willTransition 动作中,alertify 是非常异步的,ember 不会等待确认。无论您单击什么,页面都已导航。

willTransition(transition) {
  alertify.confirm('Confirm', 'Are you sure you want to navigate?', function(e) {
    if(e) {
     return true;
    } else {
     transition.abort();
    }
  });
}

请在这件事上给予我帮助!!

4

1 回答 1

0

您可以中止并重试转换。在显示确认对话框之前,您必须中止转换。确认您的对话框后,您可以重试转换并阻止您的代码再次显示您的确认对话框。所以以下应该有效(未经测试):

export default Ember.Route.extend({

  // ...

  showConfirmation: true,

  actions: {
    willTransition(transition) {
      if(!this.get('showConfirmation')) {
        this.set('showConfirmation', true);
        return true;
      }
      // Abort the transition for now
      transition.abort();

      // Now show a confirm box with alertify.
      // According to the docs, only the following 
      // is allowed: http://alertifyjs.com/confirm.html

      alertify.confirm('Are you sure you want to navigate?', () => {   
        // According to the documentation of alertify, 
        // this code is only run when you confirm your box.
        this.set('showConfirmation', false);
        transition.retry();
      });
    }
  }
}
于 2017-07-19T06:53:49.500 回答