[英]Meteor: Dynamic image in template
Is it possible to create a template with a dynamic loading image? 是否可以使用动态加载图像创建模板?
template 模板
<template name="example">
<img src="{{src}}">
</template>
helper 帮手
Template.example.helpers({
src: function() {
return Collection.findOne({}, { sort: { timestamp: -1 }}).url;
}
});
As you can see the src-url is stored in a collection document and I always choose the newest document of the collection. 如您所见,src-url存储在集合文档中,我总是选择集合的最新文档。
But if I insert a new document in that collection nothing happens until I do a reload of the page. 但是,如果我在该集合中插入新文档,则在重新加载页面之前不会发生任何事情。
MongoDb cursors are reactive not the data itself. MongoDb游标是反应性的,而不是数据本身。 In your helper you are basically fetching
one document from the database using findOne()
. 在您的助手中,您基本上是使用findOne()
从数据库中fetching
一个文档。 findOne()
get one document and closes the cursor. findOne()
获得一个文档并关闭光标。 You can try find()
with limit
and show it in the dom using #each
property of Blaze. 您可以尝试使用find()
进行limit
并使用Blaze的#each
属性在dom中显示它。
Collection.find({}, {limit:1 , sort: { timestamp: -1 }});
{{#each src }}
<img src="{{src.url}}">
{{/each}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.