【发布时间】:2016-02-27 08:34:13
【问题描述】:
我用我的 Meteor 加载 Iron:Router,现在我在加载所有集合和一个特定条目时遇到了困难。
我正在尝试做的事情:在导航栏中,我想列出用户以前的所有条目。我得到了很好的工作。它将每一个都列出到一个下拉列表中,提供一个正确的链接,并且只加载用户之前输入的内容。在正文中,它假设为每个条目加载特定信息。
什么不起作用:它要么没有给我一个 findOne() ,其中 params._id 等于路线中的 id,要么它没有加载任何东西。位置是正确的。它只是没有按应有的方式加载信息。
更新:我移动了一些东西并打印了“名称”字段,但仍然无法让它验证所有者。控制台打印出:“模板助手中的异常:ReferenceError: currentCharacter is not defined”替换为当前代码。
我做错了什么?下面是代码:
路线:
this.route('character/:_id', {
template: 'character',
subscriptions: function() {
return Meteor.subscribe("characterlist");
return Meteor.subscribe("characterlist", this.params._id);
},
data: function () {
var routeid = this.params._id;
return {
currentCharacter: CharList.findOne({_id: routeid}),
characterlist: CharList.find({'owner':Meteor.userId()})
};
}
});
模板助手类:
Template.character.helpers({
characterlist: function () {
return CharList.find({}, {sort: {createdAt: -1}});
},
currentCharacter: function () {
return CharList.findOne({'_id':Router.current().params._id});
},
isOwner: function () {
return currentCharacter.owner === Meteor.userId();
}
});
HTML:
<template name='character'>
<div class="container-body">
{{#if currentUser}}
<div class="well">
{{currentCharacter.name}}
{{#with currentCharacter}}
{{#if isOwner}}
<p>Character: {{name}}</p>
{{else}}
<p>You are not approved to make spends for this character.</p>
{{/if}}
{{/with}}
</div>
{{else}}
<h4>Please log in.</h4>
{{/if}}
</div>
</template>
【问题讨论】:
标签: meteor iron-router