【发布时间】:2016-04-06 16:39:19
【问题描述】:
我展示了一个项目列表,每个项目都属于一个用户。我列出了项目的名称、描述和创建它的用户(按名称)。我还希望有一个指向创建该项目的用户的链接。注意:我还链接到一个项目的页面。
这是我到目前为止所做的:
包
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
client/items/listed_item.html
<template name="listedItem">
<a href="{{pathFor 'itemPage'}}">
{{name}}
</a>
<a href="{{pathFor 'profile'}}">
{{usernameFromId user}}
</a>
</template>
客户端/items/items.html
<template name="items">
<div class="items">
{{#each items}}
{{> listedItem}}
{{/each}}
</div>
</template>
client/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,因此它链接到不存在的用户。即items/1234 = users/1234 当系统中只有 users/1 时。如何让它链接到正确的用户 ID?
【问题讨论】:
-
只是一个问题:在client/helpers.js中你有一个参数userId,但在函数内部你使用this.userId。您是否打算使用 userId 参数查找用户?
-
@JosHarink 是的,没错。我希望那里的助手能够为任何模板查找用户字段。
标签: meteor meteor-blaze meteor-accounts meteor-helper