【发布时间】:2018-07-15 15:34:05
【问题描述】:
我有一个计算属性来检查用户是否已经喜欢过帖子:
likeable: Ember.computed(function () {
const currentUser = this.get('auth.credentials.id');
const post = this.get('post.id');
// Get likes and filter by post and current user
this.get('store').query('like', {
filter: {
user_id: currentUser,
post_id: post
}
}).then(function(like) {
// Check if any likes were returned
if (like.get('length') != 0) {
console.log('length is not 0')
return false
} else if (like.get('length') === 0) {
console.log('length is 0')
return true
}
})
})
它在我的模板文件中被这样调用:
{{#if likeable}}
<button class="like-button" {{action 'addLike' post}}>Like This Post</button>
{{/if}}
我能够看到 console.logs 并且我知道正在检索正确的记录。但是,问题是 likeable 的值没有更新,并且按钮永远不会显示。我很确定这是因为我的条件在回调中,而不是对实际的计算属性返回 true。有没有解决的办法?
【问题讨论】:
标签: ember.js callback handlebars.js computed-properties