My view is like this :
<div class="favorite" style="margin-bottom:5px;">
@if (Auth::user())
<add-favorite-store :id-store="{{ $store->id }}"></add-favorite-store>
@else
<a href="javascript:" class="btn btn-block btn-success">
<span class="fa fa-heart"></span> Favorite
</a>
@endif
</div>
My component is like this :
<template>
<a href="javascript:" class="btn btn-block btn-success" @click="addFavoriteStore($event)">
<span class="fa fa-heart"></span> <label id="favoriteId">{{ store_id == 'responseFound' ? 'Un-Favorite' : 'Favorite' }}</label>
</a>
</template>
<script>
export default{
props:['idStore'],
mounted(){
this.checkFavoriteStore()
},
methods:{
addFavoriteStore(event){
var label = $('#favoriteId')
var text = label.text()
event.target.disabled = true
const payload= {id_store: this.idStore}
if(text == "Favorite") {
this.$store.dispatch('addFavoriteStore', payload)
}
else {
this.$store.dispatch('deleteFavoriteStore', payload)
}
setTimeout(function () {
location.reload(true)
}, 1500)
},
checkFavoriteStore(){
const payload= {id_store: this.idStore}
var data = this.$store.dispatch('checkFavoriteStore', payload)
data.then((res) => this.store_id = res)
}
},
data() {
return {
store_id: ''
}
}
}
</script>
On the my code above, I using
location.reload(true)
to update data on the page. But it's reload the page.
I want when update the page, it's not reload the page
How can I do it?
What you need is vm-forceUpdate.
I haven't tried using this myself, just came across it while working on a project.
vue forceUpdate or you could just call the function in the
setTimeout
method.You are using a lot of javascript here, you can use vue to do most of the work there.
Another solution explained in vue-router doc. Simply use the
watch
system applied to the$route
native attribute. You'll be able to detect new route even if they use the same component and fetch the data in consequence.Theres a lot thats not mentioned here to help answer the question as good as we could, its needs the html in question, what need to update and to what etc.
I think it's best you look at this https://laracasts.com/series/learn-vue-2-step-by-step for a good understanding of how Vuejs works and how to do simple things like, this.
Your using in-depth store/vuex type coding and if your not understanding the basics then it can get hard to work out the answer for you.
Sorry to be a bit blah but.
Ok Here is a simple use case but less complicated as yours and using vuejs as it should be used. (http://codepen.io/simondavies/pen/MJOQEW)
OK let laravel/php code get the store ID as well as if its already been favorited. This way your script is not first checking the store to then decide what to do.
What this does is sends the
store-id
and theis-favorited
through the component like:Then the Vue component will update the button to display if its already liked (red) or not (grey), and then also handle the click event and update accordingly as well.
As you are using Laravel to tell the component if it's already favorited you can get rid of your checking function and one less http request. Then you only need to then update the store when the user clicks the favourite button.
And as seen in the demo it updates, no need to refresh.
I hope this help you re-write yours so you get waht you want.
PS i have left out the Logged IN check
@if (Auth::user())
you have so you can put that back in etc