I'm having problems understanding Meteors reactivity. The helper works perfectly, when a task is added it is rendered in the template. However the autorun is not working, the tasks are only logged the first time.
Both the helper and the autorun are reactive computations so both should run when the Tasks collection changes?
import { Template } from 'meteor/templating';
import { Tasks } from '../../api/tasks';
import './day.html';
Meteor.subscribe('tasks');
Tracker.autorun(function() {
var tasks = Tasks.find({});
console.log(tasks);
});
Template.day.helpers({
tasks() {
return Tasks.find({});
}
});
According to Meteor docs:
Cursors are a reactive data source. On the client, the first time you retrieve a cursor’s documents with fetch, map, or forEach inside a reactive computation (eg, a template or autorun), Meteor will register a dependency on the underlying data.
With the helper you are iterating on the template so you are registering a dependency, however with Collection.find()
you don't. If you try Tasks.find().fetch()
or Tasks.find().count()
you will see the result printed every time there is a change in the collection, because you now have a dependency, which will trigger a recomputation.