我在炭烬中有一个计算的属性

  isCurrentUserLikeIt: Ember.computed('[email protected]_id', function () {
    const userId = parseInt(this.commonService.getId());
    const likestatuses = this.get('likestatuses');

    return likestatuses.filterBy('user_id', userId).get('length') !== 0;
  })


用户调用操作时,必须更新计算出的属性。

this.doSave("likestatus", likestatus).then(function (result) {
        if (result.userlike) {
          const likestatuses = context.get('likestatuses');
          likestatuses.push(likestatusItem);
          context.set('likestatuses', likestatuses);
        } else {
          const likestatuses = context.get('likestatuses').filter(function (item) {
            return item.user_id !== userId
          });
          context.set('likestatuses', likestatuses);
        }

        context.debug(context.get('likestatuses'));
      });


isCurrentUserLikeIt中的语句位于else中时,this.doSave属性将更新。但是当语句在if (result.userlike)中时,isCurrentUserLikeIt属性没有改变:(

谁能解决这个问题

最佳答案

if检查中,您需要使用pushObject而不是push,这将触发计算属性重新计算,观察者触发并将模板更新为新值。

likestatuses.pushObject(likestatusItem);

10-06 04:28