Mongodb Meteor在模板助手函数中使用fetch或find?
在meteor template helper函数中,如果我返回Mongodb Meteor在模板助手函数中使用fetch或find?,mongodb,meteor,Mongodb,Meteor,在meteor template helper函数中,如果我返回find与fetch的结果,在性能、重新渲染次数或其他方面是否有任何差异 例如,查找方法: Template.players.topScorers = function () { return Users.find({score: {$gt: 100}}, {sort: {score: -1}}); }; 或添加提取: Template.players.topScorers = function () { return U
find
与fetch
的结果,在性能、重新渲染次数或其他方面是否有任何差异
例如,查找方法:
Template.players.topScorers = function () {
return Users.find({score: {$gt: 100}}, {sort: {score: -1}});
};
或添加提取:
Template.players.topScorers = function () {
return Users.find({score: {$gt: 100}}, {sort: {score: -1}}).fetch();
};
只查找的方法是当前的方法,但是我看到很多其他人使用fetch
是的
通过使用fetch,您可以当场注册对整个查询结果集的依赖关系。通过使用find
,然后使用{{{{each}}
在每个文档上分别注册依赖项。因此,当一个文档发生更改时,只会重新呈现相关代码。使用fetch
时,更改结果集中的任何文档都将重新呈现使用fetch
的整个范围
对于较小的结果集,这没有任何区别。对于频繁变化的较大集合,它可能会降低计算速度并导致不需要的视觉伪影
我写了一篇文章,可能会帮助你理解它(虽然它不会直接回答你的问题)这是我们在Oodles Technologies中遵循的 要定义帮助程序,只需转到模板js文件(例如,如果模板名为allInventory),那么只需转到allInventory.js文件并按如下方式编写帮助程序:-
Template.allInventory.helpers({
})
在此帮助器中创建一个函数,您可以将从数据库、会话或其他服务获取数据的逻辑放入其中,然后在html中使用该函数,如:-
Template.allInventory.helpers({
productDetails: function() {
return Session.get('dbData');
}
})
On html side you just need to use the function name as follows:-
{{#each productInfo in productDetails}}
<div class="imgb"><img src="{{productInfo.image_url}}"></div>
{{productInfo.item_name}}
{{productInfo.seller_sku}}
{{productInfo.quantity}}
{{productInfo.price}}
<a type="button" class="full-view text-success"><i id="fullView" data="{{productInfo._id}}" class="fa fa-eye"></i></a>
{{/each}}
Template.allInventory.helpers({
productDetails:函数(){
return Session.get('dbData');
}
})
在html端,您只需使用函数名,如下所示:-
{{{#productDetails中的每个productInfo}
{{productInfo.item_name}
{{productInfo.seller_sku}}
{{productInfo.quantity}
{{productInfo.price}
{{/每个}}
正如您在上面的productDetails中所看到的,可以通过该名称直接访问helper类中的函数名,您可以通过Html模板中的每个循环遍历该函数名,在该函数名上获取要在Html上呈现的数据