如何路由到用户个人资料页面?

时间:2016-01-02 16:15:23

标签: meteor meteor-blaze meteor-accounts meteor-helper

我显示了一个项目列表,以及每个项目属于一个用户。我列出了项目的名称,描述和创建它的用户(按名称)。我还希望有一个链接指向创建该项目的用户。注意:我还链接到项目的页面。

这是我到目前为止所做的:

  

aldeed:collection2
aldeed:simple-schema
iron:router
aldeed:autoform
useraccounts:iron-routing
accounts-password
accounts-base
useraccounts:core
zimme:active-route
todda00:friendly-slugs
reywood:publish-composite
  

的客户机/项目/ listed_item.html

<template name="listedItem">
  <a href="{{pathFor 'itemPage'}}">
    {{name}}
  </a>
  <a href="{{pathFor 'profile'}}">
    {{usernameFromId user}}
  </a>
</template>
  

的客户机/项目/ items.html

<template name="items">
<div class="items">
        {{#each items}}
            {{> listedItem}}
        {{/each}}
</div>
</template>
  

的客户机/ subscriptions.js

Meteor.subscribe('items');
Meteor.subscribe('allUsernames');
Meteor.subscribe('users');
  

的客户机/ helpers.js

Template.items.helpers({
  items: function() {
    return Items.find();
  },
});

Template.registerHelper("usernameFromId", function (userId) {
  var user = Meteor.users.findOne({_id: this.userId});
  return user.profile.name;
});
  

服务器/ publications.js

Meteor.publish("items", function () {
  return Items.find();
});

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

Meteor.publish("allUsernames", function () {
  return Meteor.users.find({}, { 
    fields: { 'profile.name': 1, 'services.github.username': 1  }
  });
});
  

LIB / routes.js

Router.route('/items', function () {
  this.render('items');
});

Router.route('/users/:_id', {
  template: 'profile',
  name: 'profile',
  data: function() {
    return Meteor.users.findOne({_id: this.params._id});
  }
});

这样做会显示用户URL的项目URL,因此它链接到不存在的用户。 ie items/1234 = users/1234当系统中只有用户/ 1时。如何将其链接到正确的用户ID?

4 个答案:

答案 0 :(得分:1)

使用带有构建网址的链接

<template name="listedItem">
  <a href="{{pathFor 'itemPage'}}">
    {{name}}
  </a>
  <a href="http://yourdomain.com/users/{{user}}">
    {{usernameFromId user}}
  </a>
</template>

答案 1 :(得分:1)

在这部分:

Router.route('/users/:_id', {
  template: 'profile',
  name: 'profile',
  data: function() {
  return Meteor.users.findOne({_id: this.params._id});
 }
});

您已引用该路线并创建了一个名为“profile”的模板路径,但我看不到任何具有该名称的模板。有可能你忘了把它包括在这里吗?或者你真的没有那个模板?

确保。

答案 2 :(得分:1)

试试这个:

Router.route('/users/:_id', {
 template: 'profile',
 name: 'profile'
}

在帮助程序中,您可以通过以下方式检索该记录ID:

Router.current().params._id

答案 3 :(得分:1)

我通常使用这种方式: 在模板中:

<div class="css-light-border">
    <a href="/document/{{_id}}">{{title}}</a>
</div><!-- / class light-border -->

在router.js文件中:

Router.route('/document/:_id', function(){
    Session.set('docid', this.params._id);
    this.render('navbar', {to: "header"});
   this.render('docItem', {to: "main"});
  }); 

基本上,docid是我要显示的文档的_id。以下是您从数据库中提取的示例:

somehelper:function(){
        var currentId = Session.get('docid');
        var product = Products.findOne({_id:currentId});
        return product; 

我认为这是一种简单的方法,因为它使用Sessions。