Prevent $emit from emitting more than once in Vue












0















I have an emit call within my Vue project to update search results, but the emit is being called at least 4 times, because the emit call is defined at various spots, so the data is sometimtes emitted with it and at other spots it is not.



I am using a global bus to perform an emit.



this.$root.bus.$emit('resetValuesInInfiniteLoader', filteredEntities);

this.$root.bus.$on('resetValuesInInfiniteLoader', function (filteredEntities) {});


I tried to name the emits calls differently, and also tried to use a different global vue bus but both options did not really work well.
Any idea how I can do this in an efficient manner so that the $emit is always only called once? How do I need to set it up to ensure that the emit is always only called once? I also tried using $once, which did not work, or tried to destroy the $emit. Can someone give me a small fiddle example or so maybe so I understand how to do this in the right way?










share|improve this question



























    0















    I have an emit call within my Vue project to update search results, but the emit is being called at least 4 times, because the emit call is defined at various spots, so the data is sometimtes emitted with it and at other spots it is not.



    I am using a global bus to perform an emit.



    this.$root.bus.$emit('resetValuesInInfiniteLoader', filteredEntities);

    this.$root.bus.$on('resetValuesInInfiniteLoader', function (filteredEntities) {});


    I tried to name the emits calls differently, and also tried to use a different global vue bus but both options did not really work well.
    Any idea how I can do this in an efficient manner so that the $emit is always only called once? How do I need to set it up to ensure that the emit is always only called once? I also tried using $once, which did not work, or tried to destroy the $emit. Can someone give me a small fiddle example or so maybe so I understand how to do this in the right way?










    share|improve this question

























      0












      0








      0








      I have an emit call within my Vue project to update search results, but the emit is being called at least 4 times, because the emit call is defined at various spots, so the data is sometimtes emitted with it and at other spots it is not.



      I am using a global bus to perform an emit.



      this.$root.bus.$emit('resetValuesInInfiniteLoader', filteredEntities);

      this.$root.bus.$on('resetValuesInInfiniteLoader', function (filteredEntities) {});


      I tried to name the emits calls differently, and also tried to use a different global vue bus but both options did not really work well.
      Any idea how I can do this in an efficient manner so that the $emit is always only called once? How do I need to set it up to ensure that the emit is always only called once? I also tried using $once, which did not work, or tried to destroy the $emit. Can someone give me a small fiddle example or so maybe so I understand how to do this in the right way?










      share|improve this question














      I have an emit call within my Vue project to update search results, but the emit is being called at least 4 times, because the emit call is defined at various spots, so the data is sometimtes emitted with it and at other spots it is not.



      I am using a global bus to perform an emit.



      this.$root.bus.$emit('resetValuesInInfiniteLoader', filteredEntities);

      this.$root.bus.$on('resetValuesInInfiniteLoader', function (filteredEntities) {});


      I tried to name the emits calls differently, and also tried to use a different global vue bus but both options did not really work well.
      Any idea how I can do this in an efficient manner so that the $emit is always only called once? How do I need to set it up to ensure that the emit is always only called once? I also tried using $once, which did not work, or tried to destroy the $emit. Can someone give me a small fiddle example or so maybe so I understand how to do this in the right way?







      vue.js eventemitter bus






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 20 '18 at 23:03









      BennyBenny

      144111




      144111
























          2 Answers
          2






          active

          oldest

          votes


















          0














          I have found this to be the case also and feel that there are some problems with using it in multiple locations. My understanding is that global event busses are not recommended in most applications as they can lead to a confusing tangle of events. The recommendation is that you use a state management solution like vuex.



          But anyway, just a couple of points with your code above. I don't know how you created your bus but I have known to create it as such:



          //main.js



          const EventBus = new Vue()
          Object.defineProperties(Vue.prototype, {
          $bus: {
          get: function () {
          return EventBus
          }
          }
          })


          This creates it and makes it global. It can then be triggered in a component or components with:



          <button @click="$bus.$emit('my-event')">click</button>

          or

          methods: {
          triggerMyEvent () {
          this.$bus.$emit('my-event', { ... pass some event data ... })
          }
          }


          and listened to:



          created () {
          this.$bus.$on('my-event', ($event) => {
          console.log('My event has been triggered', $event)
          this.eventItem = 'Event has now been triggered'
          //this.$bus.$off('my-event')
          })

          },


          I have found that it works sometimes. I don't know why but it will work then it will trigger several events and I think it is because it isn't finalised or something. You may note I have commented out this.$bus.off which certainly stops it but it then doesn't work again. So I don't know what that's all about.



          So there you go, a total non-answer, as in, Yes I've had that too, No I cant fix it.






          share|improve this answer
























          • Yeah, I do agree what you wrote makes a whole lot of sense, I now went for using vuex store, and I must say it is a lot easier to track than those global "emit" events, guess they only make sense for simple and singular communication between components

            – Benny
            Nov 26 '18 at 2:02



















          0














          I went with using vuex store, it seems a lot easier to communicate with any component within the application, has the advantage of global communication, yet does not have the caveat of sending multiple actions such as emit events






          share|improve this answer























            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',
            autoActivateHeartbeat: false,
            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%2f53402923%2fprevent-emit-from-emitting-more-than-once-in-vue%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            I have found this to be the case also and feel that there are some problems with using it in multiple locations. My understanding is that global event busses are not recommended in most applications as they can lead to a confusing tangle of events. The recommendation is that you use a state management solution like vuex.



            But anyway, just a couple of points with your code above. I don't know how you created your bus but I have known to create it as such:



            //main.js



            const EventBus = new Vue()
            Object.defineProperties(Vue.prototype, {
            $bus: {
            get: function () {
            return EventBus
            }
            }
            })


            This creates it and makes it global. It can then be triggered in a component or components with:



            <button @click="$bus.$emit('my-event')">click</button>

            or

            methods: {
            triggerMyEvent () {
            this.$bus.$emit('my-event', { ... pass some event data ... })
            }
            }


            and listened to:



            created () {
            this.$bus.$on('my-event', ($event) => {
            console.log('My event has been triggered', $event)
            this.eventItem = 'Event has now been triggered'
            //this.$bus.$off('my-event')
            })

            },


            I have found that it works sometimes. I don't know why but it will work then it will trigger several events and I think it is because it isn't finalised or something. You may note I have commented out this.$bus.off which certainly stops it but it then doesn't work again. So I don't know what that's all about.



            So there you go, a total non-answer, as in, Yes I've had that too, No I cant fix it.






            share|improve this answer
























            • Yeah, I do agree what you wrote makes a whole lot of sense, I now went for using vuex store, and I must say it is a lot easier to track than those global "emit" events, guess they only make sense for simple and singular communication between components

              – Benny
              Nov 26 '18 at 2:02
















            0














            I have found this to be the case also and feel that there are some problems with using it in multiple locations. My understanding is that global event busses are not recommended in most applications as they can lead to a confusing tangle of events. The recommendation is that you use a state management solution like vuex.



            But anyway, just a couple of points with your code above. I don't know how you created your bus but I have known to create it as such:



            //main.js



            const EventBus = new Vue()
            Object.defineProperties(Vue.prototype, {
            $bus: {
            get: function () {
            return EventBus
            }
            }
            })


            This creates it and makes it global. It can then be triggered in a component or components with:



            <button @click="$bus.$emit('my-event')">click</button>

            or

            methods: {
            triggerMyEvent () {
            this.$bus.$emit('my-event', { ... pass some event data ... })
            }
            }


            and listened to:



            created () {
            this.$bus.$on('my-event', ($event) => {
            console.log('My event has been triggered', $event)
            this.eventItem = 'Event has now been triggered'
            //this.$bus.$off('my-event')
            })

            },


            I have found that it works sometimes. I don't know why but it will work then it will trigger several events and I think it is because it isn't finalised or something. You may note I have commented out this.$bus.off which certainly stops it but it then doesn't work again. So I don't know what that's all about.



            So there you go, a total non-answer, as in, Yes I've had that too, No I cant fix it.






            share|improve this answer
























            • Yeah, I do agree what you wrote makes a whole lot of sense, I now went for using vuex store, and I must say it is a lot easier to track than those global "emit" events, guess they only make sense for simple and singular communication between components

              – Benny
              Nov 26 '18 at 2:02














            0












            0








            0







            I have found this to be the case also and feel that there are some problems with using it in multiple locations. My understanding is that global event busses are not recommended in most applications as they can lead to a confusing tangle of events. The recommendation is that you use a state management solution like vuex.



            But anyway, just a couple of points with your code above. I don't know how you created your bus but I have known to create it as such:



            //main.js



            const EventBus = new Vue()
            Object.defineProperties(Vue.prototype, {
            $bus: {
            get: function () {
            return EventBus
            }
            }
            })


            This creates it and makes it global. It can then be triggered in a component or components with:



            <button @click="$bus.$emit('my-event')">click</button>

            or

            methods: {
            triggerMyEvent () {
            this.$bus.$emit('my-event', { ... pass some event data ... })
            }
            }


            and listened to:



            created () {
            this.$bus.$on('my-event', ($event) => {
            console.log('My event has been triggered', $event)
            this.eventItem = 'Event has now been triggered'
            //this.$bus.$off('my-event')
            })

            },


            I have found that it works sometimes. I don't know why but it will work then it will trigger several events and I think it is because it isn't finalised or something. You may note I have commented out this.$bus.off which certainly stops it but it then doesn't work again. So I don't know what that's all about.



            So there you go, a total non-answer, as in, Yes I've had that too, No I cant fix it.






            share|improve this answer













            I have found this to be the case also and feel that there are some problems with using it in multiple locations. My understanding is that global event busses are not recommended in most applications as they can lead to a confusing tangle of events. The recommendation is that you use a state management solution like vuex.



            But anyway, just a couple of points with your code above. I don't know how you created your bus but I have known to create it as such:



            //main.js



            const EventBus = new Vue()
            Object.defineProperties(Vue.prototype, {
            $bus: {
            get: function () {
            return EventBus
            }
            }
            })


            This creates it and makes it global. It can then be triggered in a component or components with:



            <button @click="$bus.$emit('my-event')">click</button>

            or

            methods: {
            triggerMyEvent () {
            this.$bus.$emit('my-event', { ... pass some event data ... })
            }
            }


            and listened to:



            created () {
            this.$bus.$on('my-event', ($event) => {
            console.log('My event has been triggered', $event)
            this.eventItem = 'Event has now been triggered'
            //this.$bus.$off('my-event')
            })

            },


            I have found that it works sometimes. I don't know why but it will work then it will trigger several events and I think it is because it isn't finalised or something. You may note I have commented out this.$bus.off which certainly stops it but it then doesn't work again. So I don't know what that's all about.



            So there you go, a total non-answer, as in, Yes I've had that too, No I cant fix it.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 21 '18 at 3:40









            Andrew1325Andrew1325

            37828




            37828













            • Yeah, I do agree what you wrote makes a whole lot of sense, I now went for using vuex store, and I must say it is a lot easier to track than those global "emit" events, guess they only make sense for simple and singular communication between components

              – Benny
              Nov 26 '18 at 2:02



















            • Yeah, I do agree what you wrote makes a whole lot of sense, I now went for using vuex store, and I must say it is a lot easier to track than those global "emit" events, guess they only make sense for simple and singular communication between components

              – Benny
              Nov 26 '18 at 2:02

















            Yeah, I do agree what you wrote makes a whole lot of sense, I now went for using vuex store, and I must say it is a lot easier to track than those global "emit" events, guess they only make sense for simple and singular communication between components

            – Benny
            Nov 26 '18 at 2:02





            Yeah, I do agree what you wrote makes a whole lot of sense, I now went for using vuex store, and I must say it is a lot easier to track than those global "emit" events, guess they only make sense for simple and singular communication between components

            – Benny
            Nov 26 '18 at 2:02













            0














            I went with using vuex store, it seems a lot easier to communicate with any component within the application, has the advantage of global communication, yet does not have the caveat of sending multiple actions such as emit events






            share|improve this answer




























              0














              I went with using vuex store, it seems a lot easier to communicate with any component within the application, has the advantage of global communication, yet does not have the caveat of sending multiple actions such as emit events






              share|improve this answer


























                0












                0








                0







                I went with using vuex store, it seems a lot easier to communicate with any component within the application, has the advantage of global communication, yet does not have the caveat of sending multiple actions such as emit events






                share|improve this answer













                I went with using vuex store, it seems a lot easier to communicate with any component within the application, has the advantage of global communication, yet does not have the caveat of sending multiple actions such as emit events







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 26 '18 at 2:03









                BennyBenny

                144111




                144111






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402923%2fprevent-emit-from-emitting-more-than-once-in-vue%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

                    android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

                    SQL update select statement

                    'app-layout' is not a known element: how to share Component with different Modules