我在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)
属性没有改变:(
任何人都可以解决这个问题
答案 0 :(得分:1)
在if
检查中,您需要使用pushObject
而不是push
,这将触发计算属性重新计算,观察者触发,模板将其更新为新值
likestatuses.pushObject(likestatusItem);