2014-10-03 77 views
0

我有一个模板,显示来自三个不同集合Cars,CarPaintsCarPaintTypes的文档。我知道我在路由器级需要所有这些。该模板将显示Car文档,引用Car的所有CarPaints以及分别引用返回的CarPaints(认为嵌套列表)的所有CarPaintTypes。到模板的路径需要代表Car._id的URL中的id如何在路由级别将订阅的结果用于其他订阅?

无论是Cars收集和CarPaints收集利用Car._id作为一个字段(它是Cars收集和CarPaints集合在一个领域的本地_id),这样很容易。然而,CarPaintTypes使用CarPaint._id作为它所属的CarPaint的参考。

所以我有三个出版物:

Meteor.publish('car', function(carId) { 
    return Cars.find({_id: carId}); 
}); 

Meteor.publish('carPaints', function(carId) { 
    return CarPaints.find({carId: carId}); 
}); 

Meteor.publish('carPaintTypes', function(carPaintId) { 
    return CarPaintTypes.find({carPaintId: carPaintId}); 
}); 

我的路线是这样的:

this.route('car', { 
    path: '/car/:_id', 

    waitOn: function() {  

     return [Meteor.subscribe('car', this.params._id), 
       Meteor.subscribe('carPaints', this.params._id)]; 
       // Can't figure out how to subscribe to or publish 
       // the carPaintTypes using all the results of what gets 
       // returned by 'carPaints' 
    } 
}); 

我的问题是CarPaintTypes没有Car._id作为一个字段,只是CarPaint._id引用到CarPaint文件。我在哪里以及如何将订阅的结果输入carPaints,并将每个carPaint文档返回到订阅carPaintTypes?或者是否有办法将它们全部结合在出版物中?稍后在我的帮手中做这件事情会更好吗?由于我知道我在路由级需要什么,因此所有订阅呼叫都应在路由代码中。

回答

1

你可以抓住里面Meteor.publish方法的所有3个光标和简单的返回他们:

Meteor.publish('carThings', function(carId){ 
    var carPaint = CarPaints.findOne({carId:carId}); 
    return [ 
    Cars.find({_id: carId}), 
    CarPaints.find({carId: carId}), 
    CarPaintTypes.find({carPaintId: carPaint._id}); 
    ] 
}) 

在客户端:

this.route('car', { 
    path: '/car/:_id', 

    waitOn: function() {  

     return [Meteor.subscribe('carThings', this.params._id)] 


    } 
}  
+0

在你的答案中,你只能找到一个'carPaint'文件。我问的是如何获取多个返回的'carPaint'文档,并找到与每个'carPaint'文档关联的所有'carPaintTypes'。该模板最终将显示一个嵌套树,其顶部有一个“Car”,多个“CarPaints”和每个“CarPaint”下有多个“CarPaintTypes”。 – evolross 2014-10-03 22:04:42

0

随着库巴Wyrobek的帮助下,我想通了。对于我试图实现,发布这样的容貌:

Meteor.publish('carThings', function(carId){ 
    var carPaints = CarPaints.find({carId: carId}).fetch(); 
    return [ 
     Cars.find({_id: carId}), 
     CarPaints.find({carId: carId}), 
     CarPaintTypes.find({carPaintId: {$in: _.pluck(carPaints, "_id")}}) 
    ]; 
}); 

我没有得到,你可以做你的出版物块内部操作。这是超级酷和灵活。谢谢你的帮助。