检查模板帮助程序返回的游标是否为空的最快方法? [英] Fastest way to check whether the cursor returned by a template helper is empty?
本文介绍了检查模板帮助程序返回的游标是否为空的最快方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我经常使用两次items
帮助器来做这样的事情:
I often do something like this, using the items
helper twice:
{{#if items}}
<h1>Items</h1>
{{#each items}}
{{> item}}
{{/each}}
{{/if}}
Template.foo.helpers
items: ->
Items.find
bar: true
,
sort: created: -1
transform: (item) ->
i.good = true
i
在这种情况下,Meteor是否正在做额外的工作?将if
切换为使用类似areItems
的方法会更有效吗?
Is Meteor doing extra work in this scenario? Would it be more efficient to switch the if
to use something like areItems
?
areItems: ->
Items.find
bar: true
.count() > 0
推荐答案
在模板中,您可以使用{{#with items}},然后使用'this.count'或'this.length'来检查您的帮助程序返回了任何物品.
In the template, you can use {{#with items}} and then either 'this.count' or 'this.length' to check whether your helper returned any items.
如果'items'是光标,请使用this.count. find()操作的结果:
Use this.count if 'items' is a cursor, e.g. the result of a find() operation:
{{#with items}}
{{#if this.count}}
<h1>Items</h1>
{{#each this}}
{{> item}}
{{/each}}
{{/if}}
{{/with}}
如果'items'是一个数组,请使用this.length:
Use this.length if 'items' is an array:
{{#with items}}
{{#if this.length}}
<h1>Items</h1>
{{#each this}}
{{> item}}
{{/each}}
{{/if}}
{{/with}}
这篇关于检查模板帮助程序返回的游标是否为空的最快方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文