I've been creating a forum where users can click a like button.
What I am trying to achieve is that when a user click a button I want that button to be colored to show the user already liked a comment just like Laracast.
How can I do it?So far I got
<div class="Media--footer background">
<div class="Media--footer__left">
<button @click="addOrRemoveLikes(comment.id,auth_id)"
v-if="auth_id"
>
<-------------- Here -------------------->
<i class="fa fa-thumbs-o-up" :class={'liked': --HERE-- }></i> {{ comment.likes.length }}
</button>
<div v-else>
<button
style="border: 0; background:white;"
@click="promptLogin=true"
>
<i class="fa fa-thumbs-o-up"></i> {{ comment.likes.length }}
</button>
</div>
</div>
<div class="Media--footer__right" v-for="like in comment.likes">
<a href="/@{{ like.user.name }}">{{ like.user.name }}</a>
</div>
</div>
As you can see I am using class binding, and am trying to refer to a computed prop, but I am not sure how I can do it. Also, I am passing an auth_id to the component.So, seems like I need to compare to it like
liked: function(){
return this.auth_id == like.user_id
}
But, the thing is I cannot pass an argument(comment.likes in this case) to computed props right?I need to loop through "likes" to get user_id
Sorry if I am not making myself clear.Any help would be appreciated.Thanks in advance.
----update-----
I added this code,but no luck...Why won't this work?
<button @click="addOrRemoveLikes(comment.id,auth_id)"
v-if="auth_id"
>
<i class="fa fa-thumbs-o-up" :class="{'liked': liked(comment.likes)}"></i> {{ comment.likes.length }}
</button>
Vue instance
liked:function(likes){
var self = this
likes.forEach(function(like){
return self.auth_id == like.user_id
})
}