Hi, I'm pretty new to Vue.js and I'm struggling with this particular issue.
I made a counter components that get its value from the server. This component has an update() method that I call once in the ready section of my component.
In my Vue instance I'd like to call this same update() method from time to time, when I need to reload the counters.
For now all I could do is loop through this.$children and check if the $options.name was the counter component's and if it was the case, finally call the update() method.
I'm sure there is a more vue-way to achieve this, isn't it ?
Here's my code.
<pagination-counter source="{{ url('/pages/draft') }}"></pagination-counter>
var PaginationCounter = Vue.extend({
props: ['source'],
template: '<span class="label label-default pull-right">{{ count }}</span>',
data: function() {
return {
count: 0
};
},
ready: function() {
this.update();
},
methods: {
update: function() {
this.$http.get(this.source, function(response) {
this.count = response.total;
});
}
}
});
Vue.component('pagination-counter', PaginationCounter);
var paginationApp = new Vue({
el: "#pagination-app",
data: {
//
},
ready: function() {
//
},
methods: {
fetchPage: function(page) {
this.$http.get(page).then(function(response) {
this.updateCounters();
}, function(response) {
// handle error
});
},
updateCounters: function() {
for (var i = 0; i < this.$children.length; i++) {
if (this.$children[i].$options.name == 'pagination-counter') {
this.$children[i].update();
}
}
}
}
});