Skip to content

$autorun should rely on reactive property to work #58

Open
@red-meadow

Description

@red-meadow

Recently I decided to switch to using computed properties returning this.$autorun() instead of meteor property, because of this #49 issue. But encountered another issue. Computed property returning this.$autorun() becomes reactive only if it has any reactive property (data or props) inside function body. Here are two examples illustrating the issue:

<!-- non-reactive example, always 0 -->
<template><div>{{ nonReactiveItems.length }}</div></template>

<script>
const Items = new Mongo.Collection(null)
Meteor.setInterval(() => { Items.insert({}) }, 2000)

export default {
  computed: {
    nonReactiveItems() {
      return this.$autorun(() => Items.find())
    }
  },
}
</script>
<!-- reactive example -->
<template><div>{{ reactiveItems.length }}</div></template>

<script>
const Items = new Mongo.Collection(null)
Meteor.setInterval(() => { Items.insert({}) }, 2000)

export default {
  data() { return { anything: undefined } },
  computed: {
    reactiveItems() {
      this.anything // This line enables reactivity
      return this.$autorun(() => Items.find())
    },
  },
}
</script>

Now the question is whether it is an issue at all? The only provided example in docs relies on reactive this.sort property, so probably it is an expected behavior? If so, we should update the README to make it clear.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions