vue js event bus calls few events











up vote
1
down vote

favorite












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('/')
},













share|improve this question


























    up vote
    1
    down vote

    favorite












    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('/')
    },













    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      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('/')
      },













      share|improve this question













      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('/')
      },









      //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('/')
      },






      //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('/')
      },







      vue.js vuejs2 event-bus






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 10 at 18:26









      yarden

      175




      175
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote













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



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



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






          share|improve this answer





















          • 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?
            – yarden
            Nov 10 at 20:30










          • you can console.trace() inside the $on callback function to see which componnt was triggering the event
            – Efrat
            Nov 10 at 20:36










          • 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'); })
            – Efrat
            Nov 10 at 20:43












          • 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
            – yarden
            Nov 10 at 20:54










          • 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.
            – Efrat
            Nov 10 at 21:07













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242102%2fvue-js-event-bus-calls-few-events%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote













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



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



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






          share|improve this answer





















          • 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?
            – yarden
            Nov 10 at 20:30










          • you can console.trace() inside the $on callback function to see which componnt was triggering the event
            – Efrat
            Nov 10 at 20:36










          • 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'); })
            – Efrat
            Nov 10 at 20:43












          • 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
            – yarden
            Nov 10 at 20:54










          • 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.
            – Efrat
            Nov 10 at 21:07

















          up vote
          2
          down vote













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



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



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






          share|improve this answer





















          • 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?
            – yarden
            Nov 10 at 20:30










          • you can console.trace() inside the $on callback function to see which componnt was triggering the event
            – Efrat
            Nov 10 at 20:36










          • 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'); })
            – Efrat
            Nov 10 at 20:43












          • 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
            – yarden
            Nov 10 at 20:54










          • 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.
            – Efrat
            Nov 10 at 21:07















          up vote
          2
          down vote










          up vote
          2
          down vote









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



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



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






          share|improve this answer












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



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



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







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 10 at 20:22









          Efrat

          1415




          1415












          • 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?
            – yarden
            Nov 10 at 20:30










          • you can console.trace() inside the $on callback function to see which componnt was triggering the event
            – Efrat
            Nov 10 at 20:36










          • 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'); })
            – Efrat
            Nov 10 at 20:43












          • 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
            – yarden
            Nov 10 at 20:54










          • 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.
            – Efrat
            Nov 10 at 21:07




















          • 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?
            – yarden
            Nov 10 at 20:30










          • you can console.trace() inside the $on callback function to see which componnt was triggering the event
            – Efrat
            Nov 10 at 20:36










          • 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'); })
            – Efrat
            Nov 10 at 20:43












          • 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
            – yarden
            Nov 10 at 20:54










          • 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.
            – Efrat
            Nov 10 at 21:07


















          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?
          – yarden
          Nov 10 at 20:30




          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?
          – yarden
          Nov 10 at 20:30












          you can console.trace() inside the $on callback function to see which componnt was triggering the event
          – Efrat
          Nov 10 at 20:36




          you can console.trace() inside the $on callback function to see which componnt was triggering the event
          – Efrat
          Nov 10 at 20:36












          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'); })
          – Efrat
          Nov 10 at 20:43






          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'); })
          – Efrat
          Nov 10 at 20:43














          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
          – yarden
          Nov 10 at 20:54




          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
          – yarden
          Nov 10 at 20:54












          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.
          – Efrat
          Nov 10 at 21:07






          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.
          – Efrat
          Nov 10 at 21:07




















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242102%2fvue-js-event-bus-calls-few-events%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Florida Star v. B. J. F.

          Danny Elfman

          Lugert, Oklahoma