Meteor 流星,出版:合成。如何访问模板中的关联数据?
所以我用publishComposite做了一个集合加入Meteor。我有一个具有用户id外键的父集合(订阅)。我在Meteor.users集合中查找用户名以获得实际用户名,但如何在html模板中打印此用户名呢。我的订阅数据在那里,但我如何实际引用用户名 以下是发布代码:Meteor 流星,出版:合成。如何访问模板中的关联数据?,meteor,meteor-blaze,meteor-publications,Meteor,Meteor Blaze,Meteor Publications,所以我用publishComposite做了一个集合加入Meteor。我有一个具有用户id外键的父集合(订阅)。我在Meteor.users集合中查找用户名以获得实际用户名,但如何在html模板中打印此用户名呢。我的订阅数据在那里,但我如何实际引用用户名 以下是发布代码: //publish subscriptions course view Meteor.publishComposite('adminCourseSubscriptions', function(courseId){ ret
//publish subscriptions course view
Meteor.publishComposite('adminCourseSubscriptions', function(courseId){
return {
//get the subs for the selected course
find: function(){
return Subscriptions.find(
{course_id: courseId}
);
},
children:
[
{
//get the subscriber details for the course
find: function(sub){
return Meteor.users.find({_id:sub.user_id});
}
}
]
};
});
以下是模板子说明:
Template.adminCourseDetail.helpers({
courseDetail: function(id){
var id = FlowRouter.getParam('id');
return Courses.findOne({ _id: id });
},
courseSubscriptions: function(){
var id = FlowRouter.getParam('id');
return Subscriptions.find({course_id:id})
},
users: function(){
return Meteor.users.find();
}
});
模板(即垃圾)ps课程详细信息来自一个单独的集合。单独获取细节更容易,我认为性能更高,而且效果很好。这只是我无法正确显示的用户名:
<template name="adminCourseDetail">
<h1>Course Details</h1>
<p>Title: {{courseDetail.title}}</p>
<p>Description: {{courseDetail.description}}</p>
<p>Start Date: {{courseDetail.startDate}}</p>
<p>Number of sessions: {{courseDetail.sessions}}</p>
<p>Duration: {{courseDetail.duration}}</p>
<p>Price: {{courseDetail.price}}</p>
<p>{{userTest}}</p>
<a href="#">edit</a>
<a href="#">delete</a>
<h2>Course Subscriptions</h2>
{{#each courseSubscriptions}}
<div class="row">
<div class="col-md-3">{{username}}</div>
<div class="col-md-3">{{sub_date}}</div>
</div>
{{/each}}
</template>
课程详情
标题:{{courseDetail.Title}
描述:{{courseDetail.Description}
开始日期:{{courseDetail.startDate}
会话数:{{courseDetail.sessions}
持续时间:{courseDetail.Duration}
价格:{{courseDetail.Price}
{{userTest}}
课程订阅
{{{#每门课程订阅}
{{username}}
{{sub_date}}
{{/每个}}
提前感谢您的建议 据我所知,订阅
集合的文档仅包含属性user\u id
,引用了Meteor.users
集合中相应的用户文档。如果是这种情况,则需要添加一个额外的模板帮助器,该帮助器将返回用户名:
Template.adminCourseDetail.helpers({
// ...
getUsername: function() {
if (this.user_id) {
let user = Meteor.users.find({
_id: this.user_id
});
return user && user.username;
}
return "Anonymous";
}
// ...
});
之后,只需将{{username}}
替换为{{getUsername}}
:
<template name="adminCourseDetail">
<!-- ... -->
<h2>Course Subscriptions</h2>
{{#each courseSubscriptions}}
<div class="row">
<div class="col-md-3">{{getUsername}}</div>
<div class="col-md-3">{{sub_date}}</div>
</div>
{{/each}}
<!-- ... -->
</template>
课程订阅
{{{#每门课程订阅}
{{getUsername}}
{{sub_date}}
{{/每个}}
您可能误解了包的概念。使用Meteor.publishComposite(…)
将只发布一个反应式连接,但它不会返回一组新的连接数据。对于其他有类似问题的人,请看我的特定示例。在我的例子中,以下代码起作用。根据Matthias的回答:
在模板帮助器中:
getUsername: function() {
let user = Meteor.users.findOne({
_id: this.user_id
});
return user;
}
然后在模板中:
{{getUsername.username}}
我的每个块都在从订阅集合而不是课程集合返回的游标中循环,这就是为什么它比Matthias提供的代码更简单。谢谢Matthias,我完全忘记了从每个块中调用帮助程序时,助手是在循环遍历的游标的上下文中调用的,因此可以使用“this”访问该游标中的数据。我在试图弄清楚如何将收集到的数据链接起来时,感到十分困惑。:)