JavaScript Observe to some event from another component in Vue

I have two components:

<div>
    <advertiser-add-component></advertiser-add-component>
    <advertisers-component></advertisers-component>
</div>

These components have the following interface: enter image description here

First component is a button. I will see a modal dialog when I click on it. The modal contains an Ajax form:

enter image description here

Second component is a table. You already saw it on the first picture So, I would like to rerender the table component after sending the form without page refreshing. Is there some way to "subscribe" to this event (once the modal information is sent) from advertisers-component? How I can organize it with minimum code?

Answer:1

Yes absolutely, for something like this you could spin up a simple event bus, your component would emit an update to the bus, and the other watches for updates to that event type on the bus.

Here's a good example of how to do it:

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

Answer:2

Im using vue-events package: https://github.com/cklmercer/vue-events

  • Install the package globally
  • Add events object in your component with the events to listen, and fire the event from the modal, check this example:

    Component with table Modal sending reload event

Answer:3

I would encourage you to check this: https://vuejs.org/v2/guide/state-management.html

Basically you have a component (or java class) called a store that holds the data of the table. That data is the one displayed by the table. When submitting the modal, you ask the store to fetch the data again (by sending an event) from your backend where I assume the data is stored.

Since the table displays the data of the store already, the table will update itself automatically.

I have done just that in an application of mine here: https://github.com/Draluy/Jobsearch/blob/master/front/src/components/applications/Application.vue

Answer:4

I have a question whether this is possible or not. For example, there is one code like below. But that code has an error for sure. And I want to make if is true when one data is at some array. Could ...

I have a question whether this is possible or not. For example, there is one code like below. But that code has an error for sure. And I want to make if is true when one data is at some array. Could ...

Firebase has a function 'createUserWithEmailAndPassword' that can be called from the frontend to create a user. What prevents someone from manually invoking the function from inside your javascript ...

Firebase has a function 'createUserWithEmailAndPassword' that can be called from the frontend to create a user. What prevents someone from manually invoking the function from inside your javascript ...

  1. how to prevent drug abuse
  2. what prevents animal abuse

I want to get today's date + 7 days. Today I get with public dateToday: string = new Date().toLocaleDateString('de-DE'); On Google I found this: this.dateOneWeek.setDate(this.dateOneWeek.getDate() +...

I want to get today's date + 7 days. Today I get with public dateToday: string = new Date().toLocaleDateString('de-DE'); On Google I found this: this.dateOneWeek.setDate(this.dateOneWeek.getDate() +...

  1. ionic date from string
  2. ionic date to string format
  3. ionic get date from string

I have a weird issue with ngAnimate in a simple application I'm doing with the MEAN Stack. Both angular.js and angular-animate.js are 1.4.7 version. I have an ul that looks something like this: &...

I have a weird issue with ngAnimate in a simple application I'm doing with the MEAN Stack. Both angular.js and angular-animate.js are 1.4.7 version. I have an ul that looks something like this: &...

  1. angularjs animate list
  2. angular animation list
  3. angular animation list items
  4. angular list animation example