ember.js 如何在RSVP.allSettled后更新控制器

xmq68pz9  于 2023-08-04  发布在  其他
关注(0)|答案(1)|浏览(187)

在ember控制器中完成所有的rsvp promise之后,我如何在控制器中保存所有的错误状态并更新DOM?我得到了未定义的'this',控制器不再可访问。

export default class someController extends Controller {
  @tracked errorlist = [];

  @action someAction {
    RSVP.allSettled(prommises).then(function(retarray){
      retarray.forEach((retstate) => {
        if (retstate.state == 'rejected') {
          this.errorlist.push(stateRet.reason);   
          //'this' undefined, how to update errlist?
        }
      });
    })
  }
}

字符串

w80xi6nr

w80xi6nr1#

有几件事你可以尝试,但这个(不太优雅的)解决方案应该会给予你想要的结果:

export default class someController extends Controller {
  @tracked errorlist = [];

  @action
  someAction {
    const that = this;

    RSVP.allSettled(promises).then(function (retarray) {
      retarray.forEach((retstate) => {
        if (retstate.state == 'rejected') {
          that.errorlist.push(retstate.reason);
        }
      });
    });
  }
}

字符串

就我个人而言,我会将errorlist.push移动到控制器上自己的方法中,这样你就可以调用这样的东西:

export default class someController extends Controller {
  @tracked errorlist = [];

  @action
  someAction {
    RSVP
      .allSettled(promises)
      .then((retarray) => retarray.forEach(this.addToErrorIfRejected));
  }

  @action
  addToErrorIfRejected({ state, reason }) {
    if (state == 'rejected') this.errorlist.push(reason);
  }
}

相关问题