Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/442.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么我的助手不';当我在其中调用的集合更新时,是否重新运行?_Javascript_Mongodb_Meteor_Meteor Helper_Minimongo - Fatal编程技术网

Javascript 为什么我的助手不';当我在其中调用的集合更新时,是否重新运行?

Javascript 为什么我的助手不';当我在其中调用的集合更新时,是否重新运行?,javascript,mongodb,meteor,meteor-helper,minimongo,Javascript,Mongodb,Meteor,Meteor Helper,Minimongo,我有一个视频播放器的模板。它使用助手从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

我有一个视频播放器的模板。它使用助手从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>

问题


当我在另一个模板中更新我的
项目
集合时,如何确保助手重新运行?由于我使用的是一个
集合.find()
难道它不应该是反应式的吗?

至于你的第一个问题(在你的代码注释中),如果你在任何时候只有一个文档,那么有一个更简单的方法:

您可以简单地使用以下选项,而不是为您的默认设置一个沉重的三元组:

由于您使用的是
find
(或者
findOne
,除了返回一个文档外,基本上是一样的),所以它总是被动的。如果文档发生更改,则跟踪器计算将无效,并且您的助手:

在引擎盖下,每个助手启动一个新的Tracker.autorun。当助手的反应依赖项更改时,将重新运行该助手。助手依赖于其数据上下文、传递的参数以及在执行期间访问的其他反应数据源


然而,正如在一篇评论中所指出的,您的助手会触发副作用(
loadPlayer()
)-这通常被认为是一种不好的做法,因为助手与getter非常相似:您只希望它返回数据,而不改变任何内容。您可能需要为此作业运行另一个。

您不应在模板帮助程序代码中添加任何副作用。它们应该只读取值。相反,请考虑使用<代码> OnDebug 回调和调用<代码> LoopPrror()<代码>。在这个问题中,我没有提到一个棘手的部分。我希望我的播放器对视频源不可知。为了正确设置其大小(无论源代码大小/比率是什么),我以
rendered
函数的形式加载源代码一次,然后挂接
loadedmetadata
,以便首先以正确的比率和大小加载videojs。我可能会将整个过程(在
loadedmetadata
事件之前)包装到
template.autorun()
中的
rendered
函数中,并删除帮助程序。谢谢你的评论@apendua。是的,这很有道理。太好了!非常感谢,这是明确和有意义的。当我在
呈现的
函数中将助手内容包装在
this.autorun()
中时,它就像一个符咒。我假设三元代码在代码可读性方面是“重”的,但是,请检查一下:它的性能是否较低?除了可读性差之外,在if/then/else计算的情况下,还有什么其他原因应该忽略它而采用另一种方法吗?(也就是说,不完全是我在本例中使用的那种,我同意
|
要简单得多)。@Billybobbonet Meh,知道哪一种性能更好没有多大用处。我的意思是,如果每运行1000次,你都能节省10毫秒的计算时间,你为什么会在意呢?为什么你的用户会这样做?这是一种微优化,你只会在服务器端做非常具体的用途。。。你最大的瓶颈通常是网络,而不是CPU。由于我对Javascript还是新手,我想知道在这种情况下是否有更好的做法。并不是说我特别关注性能(或这方面的微观优化)。我想我的答案在你的答案里。谢谢:-)
var currentProject = Projects.findOne();
//Always place the default on the right, since '' is falsey
return (currentProject.setup.player.item_loaded.url || '');