简体   繁体   English

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

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

I have a template with a video player. 我有一个带有视频播放器的模板。 It gets the video source from a mongo document, using an helper. 它使用帮助程序从mongo文档获取视频源。 Here is how it looks like: 看起来是这样的:

The helper: 帮手:

'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:"";
}

And the HTML looks like that: 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>

The question 问题

How can I make sure that my helper is re-run when I update my Projects collection in another template? 在另一个模板中更新我的Projects集合时,如何确保我的助手重新运行? Since I use a Collection.find() shouldn't it already be reactive? 由于我使用Collection.find()它不应该已经是反应性的吗?

As for your first question (in the comments of your code), if you only have one document at any point in time then there is a much simpler way with findOne : 至于第一个问题(在代码的注释中),如果在任何时间点只有一个文档,那么findOne方法要简单得多:

var currentProject = Projects.findOne();

Instead of a heavy ternary for your defaulting you could simply use the || 除了使用笨重的三元数作为缺省值之外,您可以简单地使用|| operator : 操作员

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

And because you use find (or findOne which is basically the same thing except it returns one document) it will always be reactive. 而且因为您使用find (或findOne ,除了返回一个文档,它基本上是相同的东西),所以它将始终是反应性的。 If the document changes, then Tracker computations will invalidate and your helper uses such a computation : 如果文档发生更改,那么Tracker的计算将无效,您的助手将使用以下计算

Under the hood, each helper starts a new Tracker.autorun. 在后台,每个帮助程序都会启动一个新的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. 帮助程序取决于其数据上下文,传递的参数以及在执行期间访问的其他反应性数据源。

However, as 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. 但是,正如注释中的附录所述,您的助手会引发副作用( loadPlayer() )-通常认为这是一种不好的做法,因为助手与吸气剂非常相似:您只希望它返回数据,而不更改任何内容。 You may want to run another Tracker computation for this job. 您可能要为此作业运行另一个Tracker计算

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM