【发布时间】:2015-08-19 15:41:24
【问题描述】:
我有每个卖家的个人资料页面,可以私下和公开访问。我已发布卖家(用户)信息并向客户发送数据,但我正在努力将卖家的产品集合发送给客户。
如果我可以将个人资料页面的用户信息发送到 Products 集合,我可以为特定卖家发布产品,但我目前陷入困境。我用这篇文章作为参考:Meteor: User Profile Page with Iron Router
这是我迄今为止所拥有的,它不是 DRY:
客户端模板
<template name="profile">
<div style="margin-top 5em;">
<h1>Profile.Name: {{profile.name}}</h1>
<h1>Username: {{username}}</h1>
<h1>Profile.Description: {{profile.description}}</h1>
{{#each products}}
<h1>Products! {{username}}</h1>
<h1>price {{price}}</h1>
{{/each}}
</div>
</template>
客户端助手
Meteor.subscribe("userProfile", this.params.username);
Template.profile.helpers({
products : function(){
return Products.find();
}
});
lib 上的 router.js
Router.route("/artist/:username", {
name:"profile",
waitOn:function(){
var username=this.params.username;
return Meteor.subscribe("userProfile", username);
return Meteor.subscribe("products-by-id", username);
},
data:function(){
var username=this.params.username;
return Meteor.users.findOne({username:username});
return Meteor.subscribe("products-by-id", username);
},
fastRender: true
});
服务器上的publications.js
Meteor.publish("userProfile",function(username){
// simulate network latency by sleeping 2s
Meteor._sleepForMs(2000);
var user=Meteor.users.findOne({
username:username
});
if(!user){
this.ready();
return;
}
if(this.userId==user._id){
}
else{
return Meteor.users.find(user._id,{
fields:{
"profile.name": 1,
"profile.description" : 1,
"_id" : 1,
"username" : 1,
"profile.username" : 1
}
});
return Products.find({username: user.username});
}
});
Meteor.publish("allProducts", function(){
return Products.find();
});
感谢您的任何意见!
【问题讨论】:
-
首先你在一个块中使用了太多的回报。你知道只有第一个会被执行。也可以尝试查看
dburles:collection-helpers,但很难给出一个好的答案,因为我几乎不明白你的问题。 -
谢谢你,这肯定会让我更接近。基本上我有用户,他们有产品要卖。对于每个用户,在他们的个人资料页面上,我想显示他们出售的产品。您指向的氛围收藏有一个作者和书籍收藏——在作者的个人资料页面上获取一位作者的所有书籍是我要解决的同一个问题。
标签: javascript meteor