Skip to content

Recalculation of async computed has incorrect context when using object form #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
half2me opened this issue Nov 15, 2018 · 3 comments
Closed

Comments

@half2me
Copy link

half2me commented Nov 15, 2018

When an async computed property is created in the following way:

asyncComputed: {
  myProp: {
    get() {},
  },
}

Calling this.$asyncComputed.myProp.update() will not set the proper context for the property to use this. Infact, this will be undefined. If asyncComputed is a function instead of an object, it works.

Here is a simple example showing the problem. When the property is calculated automatically everything works as expected, but when manually triggering an update(), this is not defined.

<template>
  <div class="hello">
    <h1>{{ test }}</h1>
    <button @click="recalc">Recalculate</button>
  </div>
</template>

<script>
export default {
  name: "HelloWorld",
  asyncComputed: {
    test: {
      get() {
        return new Promise((resolve, reject) => {
          setTimeout(() => resolve(this.msg), 1000);
        });
      },
      default: "calculating"
    }
  },
  data() {
    return {
      msg: "Welcome to Your Vue.js App"
    };
  },
  methods: {
    recalc() {
      this.$asyncComputed.test.update();
    }
  }
};
</script>

You can see this example in action here:
Edit Vue Template

@michaelzangl
Copy link
Contributor

Thanks for finding this, I can look into it in ~ 2 weeks.

@michaelzangl
Copy link
Contributor

michaelzangl commented Dec 29, 2018

The workaround seems to be fine for this.

The clean solution would be to hide the editable $asyncComputed property completely and only expose the computed property. The same way all other properties should then only be exposed as computed properties, which avoids accidental assignments to those properties and solves the current problem that using asyncComputed hides the "data should be a function" warning

I was planning to fix this last week, but due to other issues I need to postpone the fixes to around January 20.

@half2me
Copy link
Author

half2me commented Dec 29, 2018

@michaelzangl looking forward to the fix, it would be very helpful for me.

foxbenjaminfox added a commit that referenced this issue Jan 8, 2019
Fixed missing execution context during update function: Fixes #53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants