ember计算未更新

时间:2017-08-19 11:01:13

标签: javascript ember.js javascript-objects ember-components ember-controllers

我在ember中有一个计算属性

func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {

        let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(removeEvent(gesture:)))

        collectionView.addGestureRecognizer(longPressGesture)
    }

    func removeEvent(gesture: UILongPressGestureRecognizer) {
        if gesture.state == .began {
            let touchPoint = gesture.location(in: self.collectionView)
            if let indexPath = self.collectionView.indexPathForItem(at: touchPoint) {
                print(indexPath)

                let event = controller.object(at: indexPath as IndexPath)
                context.delete(event)
                collectionView.reloadData()
            }
        }
    }

当用户调用某个动作时,必须更新computed属性。

  isCurrentUserLikeIt: Ember.computed('likestatuses.@each.user_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属性会更新。但当声明在this.doSave时,if (result.userlike)属性没有改变:(

任何人都可以解决这个问题

1 个答案:

答案 0 :(得分:1)

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

likestatuses.pushObject(likestatusItem);