如何实现启用 Meteor Tracker 的功能?
How to implement a Meteor Tracker enabled feature?
我正在编写一个支持 Meteor Tracker 的模块,但我无法对其进行测试。我添加了 meteor-standalone-tracker
作为 dev 依赖,并写了一个测试用例,但是 autorun
函数只被调用了一次。
例如,我写了一个虚拟测试启发from the docs如下
import assert from 'assert';
import Tracker from 'trackr';
describe('Testing Tracker', () => {
it('should do as expected', () => {
var favoriteFood = "apples";
var favoriteFoodDep = new Tracker.Dependency;
var getFavoriteFood = function () {
favoriteFoodDep.depend();
return favoriteFood;
};
var setFavoriteFood = function (newValue) {
favoriteFood = newValue;
favoriteFoodDep.changed();
};
console.log("GET:" , getFavoriteFood());
// "apples"
var handle = Tracker.autorun(function () {
console.log("Your favorite food is " + getFavoriteFood());
});
// "Your favorite food is apples"
setFavoriteFood("mangoes");
// "Your favorite food is mangoes"
setFavoriteFood("peaches");
// "Your favorite food is peaches"
setFavoriteFood("bananas");
// "Your favorite food is bananas"
handle.stop();
setFavoriteFood("cake");
// (nothing printed)
});
});
我得到的只有输出是这样的:
console.log test/tracker.spec.js:27
GET: apples
console.log test/tracker.spec.js:31
Your favorite food is apples
我错过了什么?
我目前的解决方案是刷新所有计算。
Tracker.flush();
这将强制调用具有已更改的依赖项的 autorun
函数。
我正在编写一个支持 Meteor Tracker 的模块,但我无法对其进行测试。我添加了 meteor-standalone-tracker
作为 dev 依赖,并写了一个测试用例,但是 autorun
函数只被调用了一次。
例如,我写了一个虚拟测试启发from the docs如下
import assert from 'assert';
import Tracker from 'trackr';
describe('Testing Tracker', () => {
it('should do as expected', () => {
var favoriteFood = "apples";
var favoriteFoodDep = new Tracker.Dependency;
var getFavoriteFood = function () {
favoriteFoodDep.depend();
return favoriteFood;
};
var setFavoriteFood = function (newValue) {
favoriteFood = newValue;
favoriteFoodDep.changed();
};
console.log("GET:" , getFavoriteFood());
// "apples"
var handle = Tracker.autorun(function () {
console.log("Your favorite food is " + getFavoriteFood());
});
// "Your favorite food is apples"
setFavoriteFood("mangoes");
// "Your favorite food is mangoes"
setFavoriteFood("peaches");
// "Your favorite food is peaches"
setFavoriteFood("bananas");
// "Your favorite food is bananas"
handle.stop();
setFavoriteFood("cake");
// (nothing printed)
});
});
我得到的只有输出是这样的:
console.log test/tracker.spec.js:27
GET: apples
console.log test/tracker.spec.js:31
Your favorite food is apples
我错过了什么?
我目前的解决方案是刷新所有计算。
Tracker.flush();
这将强制调用具有已更改的依赖项的 autorun
函数。