自动运行中的流星反应
Meteor reactivity in autorun
我无法理解 Meteors 的反应性。
助手工作完美,当添加任务时,它会在模板中呈现。
但是 auto运行 不工作,任务只在第一次记录。
helper 和 auto运行 都是反应式计算,所以当 Tasks 集合发生变化时两者都应该 运行?
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({});
}
});
根据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.
使用 helper 迭代模板,因此您注册了一个依赖项,但是使用 Collection.find()
则不需要。如果您尝试 Tasks.find().fetch()
或 Tasks.find().count()
您将看到每次集合发生变化时打印的结果,因为您现在有一个依赖项,这将触发重新计算。
我无法理解 Meteors 的反应性。 助手工作完美,当添加任务时,它会在模板中呈现。 但是 auto运行 不工作,任务只在第一次记录。
helper 和 auto运行 都是反应式计算,所以当 Tasks 集合发生变化时两者都应该 运行?
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({});
}
});
根据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.
使用 helper 迭代模板,因此您注册了一个依赖项,但是使用 Collection.find()
则不需要。如果您尝试 Tasks.find().fetch()
或 Tasks.find().count()
您将看到每次集合发生变化时打印的结果,因为您现在有一个依赖项,这将触发重新计算。