EmberJS - 转换中止并重试时,transitionToRoute返回的保证未得到解决

时间:2015-08-20 08:21:31

标签: ember.js promise

我在PostController中有一个操作,其中包含以下代码。

var self = this;
this.transitionToRoute('post').promise.then(function(){
  self.set('isEditing', true);
});

但是,在PostRoute willTransition操作中,我有一个逻辑来检查dirtyComment并中止转换并要求用户确认,并视用户而定输入,将重试转换或中止转换并且不执行任何操作。我需要将此逻辑驻留在willTransition的{​​{1}}中,因为每当用户尝试转换出PostRoute路由时,都需要执行此检查。

/post

现在,当评论变脏并且转换被中止并重试时,承诺未完全填充。我知道这是因为willTransition: function(transition) { if (this.controller.get('isCommentDirty')){ transition.abort(); if(confirm("Are you sure you want to abort the comment?")) { // If the user confirms perform some logic transition.retry(); } } else{ // Bubble the `willTransition` action so that // parent routes can decide whether or not to abort. return true; } } 创建了一个全新的retry对象。但是,有没有解决方法呢?

另外,我不明白的是,如果实际上ember应该处理这个问题吗?

1 个答案:

答案 0 :(得分:0)

我使用另一个承诺来解决类似情况(验证检查),我完全控制了它。但这不是你的情况。

您可以通过以下方式反转行为:创建post.edit路径或将构造逻辑移动到控制器(而不是路径),如下所示:

在PostController中:

var self = this,
    isCommentDirty = this.get('isCommentDirty');

if (isCommentDirty && confirm("Are you sure you want to abort the comment?")) {
  this.transitionToRoute('post').then(function(){
    self.set('isEditing', true);
  });
}

var self = this,
    isCommentDirty = this.get('isCommentDirty');

if (isCommentDirty && confirm("Are you sure you want to abort the comment?")) {
  this.set('isEditing', true);
  this.transitionToRoute('post');
}
顺便说一下。如果要限制编辑脏评论,可以隐藏模板中的“编辑”按钮。对于这种情况,使用'post.edit'路由,PostController中的代码将是:

if (confirm("Are you sure you want to abort the comment?")) {
  this.transitionToRoute('post.edit');
}