Javascript Iron路由器:通过meteor方法将数据传递给客户端

Javascript Iron路由器:通过meteor方法将数据传递给客户端,javascript,asynchronous,meteor,iron-router,Javascript,Asynchronous,Meteor,Iron Router,我的应用程序使用iron router:当用户点击包含通配符的某个路由时,我希望使用通配符的值调用meteor方法,并使用其返回值设置模板的数据上下文 例如: 流星法: getUserName: function(id){ return Meteor.users.findOne({_id: id}).profile.name; } 路由器: data: function(){ Meteor.call('getUserName', this.params.userId,

我的应用程序使用iron router:当用户点击包含通配符的某个路由时,我希望使用通配符的值调用meteor方法,并使用其返回值设置模板的数据上下文

例如:

流星法:

getUserName: function(id){
    return Meteor.users.findOne({_id: id}).profile.name;
}
路由器:

data: function(){
        Meteor.call('getUserName', this.params.userId, function(error, result){

        });
    }
meteor方法返回正确的值,我可以在回调函数中访问该值。但我的问题是,我不知道如何实际使用这些数据。仅仅从回调中返回它是不起作用的

正确的方法是什么?或者在这种情况下,调用Meteor方法根本不是一个好主意?那么,还有什么选择呢


非常感谢您的回答

您可以使用以下方法更新视图:

Meteor.call("getUserName",this.params.userId,  function(error,result){
  if(error)  {
    throw new Error("Cannot get userName");
    return;      
  }

  Session.set("userName",result)
})
视图:

若用户将更改其姓名,则在用户再次打开路由之前,上述方法不会更新
用户名

然而,我认为更好的方法是使用meteor发布/订阅方法。 在下面的解决方案中,每当用户名在mongo中更改时,用户名将在视图中更新

Router.onBeforeAction('loading');

this.route("someRoute", {
   waitOn:function(){
     return Meteor.subscribe("getUser",this.params.userId);
   },
   data:function(){
      var user = Meteor.users.findOne({_id: this.params.userId});
      var userName = user && user.profile && user.profile.name;
      return{
        userName: userName
      }
   }
})
和在服务器上:

Meteor.publish("getUser",function(userId){
  return Meteor.users.find(userId,{fields:{profile:1}});
})
在模板
someRoute
中,通过键入以下内容显示
userName

{{userName}}

您可以使用以下方法更新视图:

Meteor.call("getUserName",this.params.userId,  function(error,result){
  if(error)  {
    throw new Error("Cannot get userName");
    return;      
  }

  Session.set("userName",result)
})
视图:

若用户将更改其姓名,则在用户再次打开路由之前,上述方法不会更新
用户名

然而,我认为更好的方法是使用meteor发布/订阅方法。 在下面的解决方案中,每当用户名在mongo中更改时,用户名将在视图中更新

Router.onBeforeAction('loading');

this.route("someRoute", {
   waitOn:function(){
     return Meteor.subscribe("getUser",this.params.userId);
   },
   data:function(){
      var user = Meteor.users.findOne({_id: this.params.userId});
      var userName = user && user.profile && user.profile.name;
      return{
        userName: userName
      }
   }
})
和在服务器上:

Meteor.publish("getUser",function(userId){
  return Meteor.users.find(userId,{fields:{profile:1}});
})
在模板
someRoute
中,通过键入以下内容显示
userName

{{userName}}

您可以使用以下方法更新视图:

Meteor.call("getUserName",this.params.userId,  function(error,result){
  if(error)  {
    throw new Error("Cannot get userName");
    return;      
  }

  Session.set("userName",result)
})
视图:

若用户将更改其姓名,则在用户再次打开路由之前,上述方法不会更新
用户名

然而,我认为更好的方法是使用meteor发布/订阅方法。 在下面的解决方案中,每当用户名在mongo中更改时,用户名将在视图中更新

Router.onBeforeAction('loading');

this.route("someRoute", {
   waitOn:function(){
     return Meteor.subscribe("getUser",this.params.userId);
   },
   data:function(){
      var user = Meteor.users.findOne({_id: this.params.userId});
      var userName = user && user.profile && user.profile.name;
      return{
        userName: userName
      }
   }
})
和在服务器上:

Meteor.publish("getUser",function(userId){
  return Meteor.users.find(userId,{fields:{profile:1}});
})
在模板
someRoute
中,通过键入以下内容显示
userName

{{userName}}

您可以使用以下方法更新视图:

Meteor.call("getUserName",this.params.userId,  function(error,result){
  if(error)  {
    throw new Error("Cannot get userName");
    return;      
  }

  Session.set("userName",result)
})
视图:

若用户将更改其姓名,则在用户再次打开路由之前,上述方法不会更新
用户名

然而,我认为更好的方法是使用meteor发布/订阅方法。 在下面的解决方案中,每当用户名在mongo中更改时,用户名将在视图中更新

Router.onBeforeAction('loading');

this.route("someRoute", {
   waitOn:function(){
     return Meteor.subscribe("getUser",this.params.userId);
   },
   data:function(){
      var user = Meteor.users.findOne({_id: this.params.userId});
      var userName = user && user.profile && user.profile.name;
      return{
        userName: userName
      }
   }
})
和在服务器上:

Meteor.publish("getUser",function(userId){
  return Meteor.users.find(userId,{fields:{profile:1}});
})
在模板
someRoute
中,通过键入以下内容显示
userName

{{userName}}