如何在Meteor模板助手中访问FlowRouter订阅? [英] How to access FlowRouter subscriptions in Meteor template helpers?

查看:98
本文介绍了如何在Meteor模板助手中访问FlowRouter订阅?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎我无法在我的助手中访问FlowRouter模板订阅.你该怎么做?

it seems like I can't access a FlowRouter template subscription in my helper. How can you do this?

在我的服务器代码中:

Meteor.publish('AllUsers', function() {
    return Meteor.users.find({}, {fields: {profile: 1}});
})

在我的路由器代码中:

var userRoutes = FlowRouter.group({
    subscriptions: function(params, queryParams) {
        this.register('AllUsers', Meteor.subscribe('AllUsers'));
    },
});

在我的模板代码中:

{{#if checkFlowRouterSubs}}
    {{#each getTheUsers}}
        {{>userPartial}}
    {{/each}}
{{/if}}

在我的助手中,我有后卫":

In my helpers I have the 'guard':

checkFlowRouterSubs: function() {
    if (FlowRouter.subsReady()) {
        return true;
    };
    return false;
},

然后是getTheUsers帮助器:

And then the getTheUsers helper:

...
var users = AllUsers.find(filterObject, { sort: { 'profile.firstname': 1 } }).fetch(); // the actual query definitely works
...

但是我得到一个错误:

Exception in template helper: ReferenceError: AllUsers is not defined

我应该注意,在getTheUsers帮助器中,FlowRouter.subsReady('AllUsers')返回true

I should note that in the getTheUsers helper, FlowRouter.subsReady('AllUsers') returns true

推荐答案

所以,首先,这个:

var userRoutes = FlowRouter.group({
    subscriptions: function(params, queryParams) {
        this.register('AllUsers', Meteor.subscribe('AllUsers'));
    },
});

不是服务器代码:它是客户端代码:流路由器是客户端路由器:直观直观,但这是所有这些路由器的基础. 这里的提示是您正在订阅"此代码中的出版物,因此它是在客户端.

is NOT server code: it is Client code: the Flow-router is a client side router: counter intuitive but this is the basis of all these routers. The hint here is that you are 'subscribing' to the publication in this code, so it is on the client side.

Iron-Router既在服务器端又在客户端进行路由,因此当您到达那里时,这会使事情变得更加混乱.

Iron-Router is routing both on the server and client-side so it makes things even more confusing when you come from there.

您在这里缺少的是服务器端的publish功能.

What you are missing here is the publish function on the server side.

Meteor.publish('AllUsers', function() {
    return AllUsers.find();
});

错误

Exception in template helper: ReferenceError: AllUsers is not defined 似乎是因为您没有在客户端上定义集合

Exception in template helper: ReferenceError: AllUsers is not defined seems like because you did not define the collection on the client side

var AllUsers = Mongo.Collection('AllUsers'); //or whatever the actual collection

这篇关于如何在Meteor模板助手中访问FlowRouter订阅?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆