【发布时间】:2015-11-10 05:49:20
【问题描述】:
我有一个带有视频播放器的模板。它使用助手从 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 集合时,如何确保我的助手重新运行?既然我使用了Collection.find(),它不应该是反应式的吗?
【问题讨论】:
-
你不应该在你的模板助手代码中加入任何副作用。他们应该只读取值。相反,请考虑使用
onRendered回调并在那里调用loadPlayer()。 -
我在问题中没有提到一个棘手的部分。我希望我的播放器与视频源无关。为了正确设置它的大小(无论源大小/比率是多少),我以
rendered函数的形式加载我的源一次,然后我连接loadedmetadata以便首先以正确的比率加载videojs,然后尺寸。我可能会将整个事情(在loadedmetadata事件之前)包装到template.autorun()函数中的rendered中,并摆脱助手。感谢您的评论@apendua。 -
是的,这很有道理。
标签: javascript mongodb meteor meteor-helper minimongo