流星:将变量从铁路由器拉到模板

Meteor: Pulling variable from iron router to a template

我正在尝试从 iron router 获取两个变量,以便我基本上可以在我的模板中调用它们,例如 {{user.telephone}}{{pic.profilepic}}。 我在路由器中的代码如下。 (不起作用!)

Router.route('/profile/:_id', {
name: 'profile',
template: 'profile',
data:function(){
    return {
        user:Info.findOne({_id:this.params._id}),
        pic: Profilepics.findOne({_id:this.params._id})
    };
   },
subscriptions: function(){
    return {
        Meteor.subscribe("userInfo"),
        Meteor.subscribe( "profilepic");
},
action:function (){
    if (this.ready()){
        this.render();
    }else {
        this.render('loading');
    }
  }

});

我只能用下面的代码做一个变量。即得到 {{user.telephone}}。任何人都可以帮助我获得两个变量而不是一个变量吗?

enterRouter.route('/profile/:_id', {
name: 'profile',
template: 'profile',
data:function(){
    return {
        user:Info.findOne({_id:this.params._id})
    };
   },
subscriptions: function(){
    return Meteor.subscribe("userInfo")
},
action:function (){
    if (this.ready()){
        this.render();
    }else {
        this.render('loading');
    }
  }
 }); 

Return 多个订阅作为来自 subscriptions 函数的数组,

使用return [ Meteor.subscribe("userInfo"), Meteor.subscribe( "profilepic") ];

而不是

return { Meteor.subscribe("userInfo"), Meteor.subscribe( "profilepic");{} 不匹配。

我遇到过类似情况

data: function() {
  var gridId = this.params._gridId;
  return (People.find({gridId: gridId})
          && GridLog.find({gridId: gridId}));
},

将 waitOn() 处理程序中的订阅调用作为布尔状态的一部分

waitOn: function() {
  return (Meteor.subscribe('people', this.params._gridId)
    && Meteor.subscribe('gridlog', this.params._gridId) );
},

在 waitOn() 中将它们 && 在一起可能会取得更大的成功(看起来很奇怪)。

如果您使用的是最新版本的 iron router,我建议您将代码更新为更现代的代码。

首先你创建一个通用的应用程序控制器:

ApplicationController = RouteController.extend({
    layoutTemplate: 'DefaultLayout',
    loadingTemplate: 'loading_template',
    notFoundTemplate: '404_template',
});

然后你开始为不同的目的扩展它:

ProfileController = ApplicationController.extend({
    show_single: function() {
        this.render('profile');
    }
});

在此之后,您可以为配置文件部分创建路线

Router.route('/profile/:_id', {
    controller: 'ProfileController',
    action: 'show_single',
    waitOn: function() {
        return [
            Meteor.subscribe("userInfo"),
            Meteor.subscribe("profilepic")
        ];
    },
    subscriptions: function() {
        this.subscribe("somethingyoudontneedtowaitfor", this.params._id);
    },
    data: function() {
        if (this.ready()) {
            return {
                user: Info.findOne({
                    _id: this.params._id
                }),
                pic: Profilepics.findOne({
                    _id: this.params._id
                })
            };
        }
    }
});

它可能需要更多的代码,但它可以让您完全控制它的功能。此外,使用它,当路由器等待订阅准备就绪时,它会显示上面定义的加载模板。如果不想显示加载,则将订阅移出 waiton。