Skip to content
This repository has been archived by the owner on Mar 7, 2024. It is now read-only.

Multiple Listeners on the same event #4

Open
tanthammar opened this issue Oct 14, 2019 · 2 comments
Open

Multiple Listeners on the same event #4

tanthammar opened this issue Oct 14, 2019 · 2 comments

Comments

@tanthammar
Copy link

Is it not possible to have multiple listeners on the same event?

Each of these listeners work on their own but if I add them all, only the last is updated

BroadcasterField::make(__('Gross'),  'gross')
                ->broadcastTo('discount')
                ->hideFromIndex(),
BroadcasterField::make(__('Disc Percent'),  'disc_percent')
                ->broadcastTo('discount')
                ->hideFromIndex(),
BroadcasterField::make(__('Disc Amount'),  'disc_amount')
                ->broadcastTo('discount')
                ->hideFromIndex(),


ListenerField::make(__('Disc Sum'),  'disc_sum')
                ->listensTo('discount')
                ->hideFromIndex()
                ->calculateWith(function (Collection $values) {
                    // some calculations;
                    return $disc_sum;
                }),
ListenerField::make(__('Disc Sum Percent'),  'disc_sum_percent')
                ->listensTo('discount')
                ->hideFromIndex()
                ->calculateWith(function (Collection $values) {
                   // some calculations;
                    return $disc_sum_percent;
                }),
ListenerField::make(__('Discounted Gross'),  'discounted_gross')
                ->listensTo('discount')
                ->hideFromIndex()
                ->calculateWith(function (Collection $values) {
                    // some calculations;
                    return $disc_gross;
}),
@pauldstar
Copy link

In the listener field, at "calculated-field/resources/js/components/listener-field/FormField.vue" the following code causes the event to only affect one listener component at a time.

calculateValue: _.debounce(function() {
      this.calculating = true;

      Nova.request()
        .post(
          `/codebykyle/calculated-field/calculate/${this.resourceName}/${this.field.attribute}`,
          this.field_values
        )
        .then(response => {
          this.value = response.data.value;
          this.calculating = false;
        })
        .catch(() => {
          this.calculating = false;
        });
    }, 500),

Removing the _.debounce() function, and replacing the above code with this solved it for me:

calculateValue: function() {
    this.calculating = true;

    Nova.request()
        .post(
            `/fusion/calculated-field/calculate/${this.resourceName}/${this.field.attribute}`,
            this.fieldValues
        )
        .then(response => {
            this.broadcastHandler(response);
            this.calculating = false;
        })
        .catch(() => {
            this.calculating = false;
        });
},

Of course, it's not ideal to modify vendor/package code. So I basically followed @codebykyle instructions here, and created my own custom package, to fix this issue myself.

@jappiewent
Copy link

jappiewent commented Jan 23, 2022

@pauldstar @codebykyle

This issue can be fixed while keeping the debounce in place! This is a very useful article I used helping debug this part.

Rewrite calculate value function:

    calculateValue: function () {
      this.calculating = true;
      Nova.request()
          .post(
              `/jappiewent/calculated-field-improved/calculate/${this.resourceName}/${this.field.attribute}`,
              this.field_values
          )
          .then(response => {
            this.value = response.data.value;
            this.calculating = false;
          })
          .catch(() => {
            this.calculating = false;
          });
    },

And add the debounce to the mounted part of your component:

  mounted: function () {
    this.calculateValue = _.debounce(this.calculateValue, 500).bind(this);
    this.calculateValue();
  },

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants