当我在其中调用的集合更新时,为什么我的助手没有重新 运行?

Why my helper doesn't re-run when the collection I call inside it is updated?

我有一个带有视频播放器的模板。它使用帮助程序从 mongo 文档获取视频源。这是它的样子:

帮手:

'currentItemUrl' : function() {
    //I only have one project. Is there a better/cleaner way to obtain it than the one below?
    var currentProject = Projects.find().fetch()[0]; 
    //the function below will create an instance of videojs or update the current one (set the new source)
    loadPlayer();
    //I return the source url if I have one
    return currentProject.setup.player.item_loaded.url?currentProject.setup.player.item_loaded.url:"";
}

HTML 看起来像这样:

<video id="video_player" class="video-js vjs-default-skin" controls preload="auto"
    width = "{{videoWidth}}" height="videoHeight"
    poster="{{currentItemPoster}}">
    <source id="mp4" src="{{currentItemUrl}}" type='video/mp4' />
    <source id="webm" src="" type='video/webm' />
    <source id="ogg" src="" type='video/ogg' />
</video>

问题

当我在另一个模板中更新我的 Projects 集合时,我如何确保我的助手是 re-运行?因为我使用 Collection.find() 它不应该是反应式的吗?

关于你的第一个问题(在你的代码的注释中),如果你在任何时间点只有一个文档,那么 findOne:

有一个更简单的方法
var currentProject = Projects.findOne();

您可以简单地使用 || operator 而不是默认的重三元组:

//Always place the default on the right, since '' is falsey
return (currentProject.setup.player.item_loaded.url || '');

并且因为您使用 find(或 findOne 除了它 returns 一个文档之外基本上是相同的东西)它总是反应性的。如果文档发生变化,那么 Tracker 计算将失效并且您的助手 uses such a computation :

Under the hood, each helper starts a new Tracker.autorun. When its reactive dependencies change, the helper is rerun. Helpers depend on their data context, passed arguments and other reactive data sources accessed during execution.

不过,作为apendua noted in a comment, your helper triggers side-effects (loadPlayer()) - This is generally considered a bad practice because a helper is very similar to a getter: You just expect it to return data, not alter anything. You may want to run another Tracker computation这份工作。