【发布时间】:2013-10-20 22:48:59
【问题描述】:
如果存在某些内容,我会尝试路由到一个页面,如果不存在,则尝试路由到另一个页面。但是,我不想订阅整个集合(〜数千),因为我认为它会影响性能。我该怎么做?
我尝试了类似的方法,但由于某种原因,Meteor 在页面加载时会两次遍历路由器代码,并在重定向到项目页面之前短暂闪烁错误页面,我不希望这种情况发生。
这是我所拥有的:
router.coffee
to: (id)->
Meteor.subscribe 'item', id
item = Items.findOne id
if item
# if the item exists, then redirect to it
Session.set 'currentItemId', id
'itemPage'
else
# if not, then redirect to the sorry page
'sorryPage'
publications.coffee
Meteor.publish 'item', (id)->
return Items.find({_id: id})
订阅整个集合会影响性能,对吗?有没有更简单的方法来检查集合中的存在而不订阅它?我试图做一个 Meteor.call 来检查它的服务器端,但它没有工作并且不理想(路由器等待服务器调用..)。有没有“正确”的方法来做到这一点?
【问题讨论】:
-
您要求的功能是内置在 Iron-router (paypertise.com/meteorjs/iron-router-tutorial) 中的,您应该考虑使用它。
标签: collections meteor router subscription