I have a local database that will be updated with pusher. This database is stored in JSON and the component will load and filter out what is not needed, making it impossible to add a watcher to the raw data.
My idea (please advice me if there are better solution) was to add a listener for a custom event, then trigger this event when the DB is updated. The only event I'm able to trigger is 'CLICK', with a simple $('.vue-template').trigger('click');
This is paired with a simple @click="doSomething" on the element I chose as my target.
This is not ideal because I don't want to fetch the data on any click, but only when the DB is updated, so I've tried @customevent="doSomething" but it doesn't work
Any help?
EDIT: More code
<template>
<div class="listen listen-database-teachers"
@dbupdate="testAlert">
<span v-for="teacher in teachers"
class="pointer btn btn-sm btn-default destroy-link"
:data-href="computedHref(teacher)"
:data-person="personIdentifier(teacher.person)"
:data-personid="teacher.person_id">
{{ personIdentifier(teacher.person) }} <span class="glyphicon glyphicon-remove"></span>
</span>
</div>
</template>
<script>
export default {
props: ['circleId'],
data () {
return {
loading: false,
teachers: null,
error: null
}
},
created () {
// fetch the data when the view is created and the data is
// already being observed
this.fetchData();
},
methods: {
fetchData() {
this.error = this.teachers = this.categories = null;
this.loading = true;
var teachers = $(window).localDatabase('teachers', $(window).planner('currentSchoolId'));
var searchCircle = this.circleId,
filtered_teachers = [];
$.each(teachers, function(index, teacher){
if(teacher.membership_id == searchCircle)
filtered_teachers.push(teacher);
});
this.teachers = filtered_teachers.sort(function(a, b) {
return personIdentifier(a.person) > personIdentifier(b.person);
});
this.loading = false;
},
computedClass(teacher){
return 'pointer btn btn-sm btn-default draggable membership-draggable-'+teacher.membership_id+' destroy-link'
},
computedHref(teacher){
return window.links.destroy_membership
+ '?association-id='+teacher.id
+ '&circle-id='+teacher.circle_id;
},
testAlert: function(){
return alert('success');
}
}
}
</script>
EDIT 2: attempt to use BUS
Remember that I use Laravel Mix.
app.js
window.vue_bus = new Vue();
master.js
function handleDatabaseUpdate(){
window.vue_bus.$emit('refresh-data');
}
component
created () {
// fetch the data when the view is created and the data is
// already being observed
this.fetchData();
window.vue_bus.$on('refresh-data', this.testAlert());
},
I've isolated the relevant bits of the code. This executes testAlert() when the component is mounted, but it returns an error cbs[i] undefined when I call handleDatabaseUpdate() from browser console (on Firefox).
methods: { raiseMyEvent() { this.$event("myEvent", myData); } }replace$('.vue-template').trigger('click');withraiseMyEvent()