1

I'm struggling with event bus. I have a list of notes I display on home ('/'). I use the event bus to update the array when in 3 different places - when deleting, creating or updating a new note. After using different options(delete+create, create+update...) the functions that are triggered by the event listener are triggered few times, creating duplicate items or deleting more than it should. I am using one event bus for all of them. I am using $once and I have placed it on the created hook.

How can I see what's in the event bus and how can I clear it after using it?

//event bus def
var eventBus = new Vue();
export default eventBus;

//the main page, where notes being displayed
  template: `
    <div>
    <h1>this is the home page</h1>
    
        <div class="note-container" v-for="note in notes" :key="note.id">
        
            <component :noteData="note" :is="note.type" class="note" :style="note.style"></component>
        </div>
    </div>
    `,
    data() {
        return {
            notes: []
        }
    }, 
    created() {
        eventBus.$once('noteAdded', data => {
            var newNotes = notesService.createNewNote(data)          
            this.notes = newNotes;
            console.log('created activated');
            
        })
        eventBus.$once('noteUpdated', note=>{
            notesService.updateNote(note)
            .then(updatedNotes=>{
                this.notes=updatedNotes
               console.log('update activated');  
            })
        })
        eventBus.$once('noteDeleted', note=>{
            notesService.deleteNote(note)
            .then(notes=>{
                console.log('delete activated');
                this.notes=notes
            })
        })
        
//where update is being called from
template: `
    <div>
    <form>
    some form
        </form>
        <button  @click="handleUpdate">Update</button>
       
    </div>   `,
     methods:{
        handleUpdate(){
            eventBus.$emit('noteUpdated', this.note)     
            this.$router.push('/')   
            console.log('handle img');
            
            
            
        },
    }
    
    //where create is being called from
      handleSubmit(){
            eventBus.$emit('noteAdded', this.data)     
            this.$router.push('/')
        },
    
    

1 Answer 1

2

usually, eventBus.$off('event') should turn the event off.

https://v2.vuejs.org/v2/api/?#vm-off

https://alligator.io/vuejs/global-event-bus/

Sign up to request clarification or add additional context in comments.

6 Comments

Tried to set it to "on" instead of "once" and then use "off", didn't work for me. Is there any way I can see what events are stored in it?
you can console.trace() inside the $on callback function to see which componnt was triggering the event
to prevent executing tha callback function on, try adding if(eventBus.eventName){return;} to the beginning of your callback function: eventBus.$once('noteAdded', data => { if(eventBus.noteAdded){return;} var newNotes = notesService.createNewNote(data) this.notes = newNotes; console.log('created activated'); })
trace shows as if it's being called from the same place. I'm getting few of those: notesService.deleteNote.then.notes @ home-page.js:46 Promise.then (async) eventBus.$once.note @ home-page.js:43 on @ vue.js:2471 Vue.$emit @ vue.js:2538 handleDelete @ todo-edit.cmps.js:69 invoker @ vue.js:2029 fn._withTask.fn._withTask @ vue.js:1828 adding the condition didn't work either. any other ideas or maybe it will be a better idea to assume the problem is somewhere else in the code? thanks
do you generate the component more than one time? if you have a few same components- all of them will trigger q react to the event, and it will get fired more than once. component can also get duplicated if your using v-if on it. in this case , again, adding to the beginning of the function if(eventBus.eventName){return;} can be helpful.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.