【问题标题】:Meteor: Pulling variable from iron router to a template流星:将变量从铁路由器拉到模板
【发布时间】:2023-03-09 12:52:01
【问题描述】:

我试图从 iron 路由器 中获取两个变量,以便我可以在我的模板中调用它们,例如 {{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 [ Meteor.subscribe("userInfo"), Meteor.subscribe( "profilepic") ];
  • 您确定这两个集合具有相同的 _id 吗? Profilepics 可能有 diff id。
  • 是的。100% 确定

标签: meteor iron-router meteor-blaze


【解决方案1】:

如果您使用的是最新版本的 Iron 路由器,我建议您将代码更新为更现代的东西。

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

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
                })
            };
        }
    }
});

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

【讨论】:

    【解决方案2】:

    subscriptions函数以数组形式返回多个订阅,

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

    而不是

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

    【讨论】:

    • 这部分呢,return { user:Info.findOne({_id:this.params._id}), pic: Profilepics.findOne({_id:this.params._id}) };我可以一次使用一个变量(用户或图片),但不能同时使用。知道这是为什么吗?
    • 那部分没问题,你可以返回一个对象作为数据上下文,通过在浏览器控制台中运行查询来验证订阅
    • 是的,我验证了订阅。另外,如果我只使用 return { user:Info.findOne({_id:this.params._id}) };它可以工作,如果我对图片做同样的事情,它也可以工作,但一起就不行了
    • 尝试从 Template.profile.rendered 在 console.log 中打印 this.data
    • 嗯,它的工作。不知道我昨天做错了什么。今天似乎工作正常。但是使用 [...] 将订阅作为数组传递绝对有效!谢谢
    【解决方案3】:

    我也遇到过类似情况

    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() 中将它们 &&ing 在一起可能会取得更大的成功(看起来很奇怪)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-01
      • 2014-04-30
      • 2014-10-30
      • 2014-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-22
      相关资源
      最近更新 更多