我有两个集合 Articles
和 Categories
假设它们的文档是这样的
文章
{
"_id": "blabla",
"title" : "title",
"description" : "description",
"categoryId" : "catId"
}
类别
{
"_id": "catId",
"title" : "category",
"description" : "description"
}
我想订阅以使它们像这样
{
"_id": "blabla",
"title" : "title",
"description" : "description",
"category" : {
"title" : "category",
"description" : "description"
}
}
我尝试使用publish-composite这是我的代码。 服务器
Meteor.publishComposite('articles', {
find: function() {
return Articles.find({}, { sort: {}, limit: 10 });
},
children: [
{
find: function(article) {
return Categories.findOne({ _id: article.categoryId });
}
}
]
});
客户端 angularjs Controller 是
angular.module("dee").controller("ArticlesListCtrl", ['$scope', '$meteor', function($scope, $meteor){
$scope.articles = $meteor.collection(Articles).subscribe('articles');
}]);
并且 View 是
{{ articles | json }}
问题是它只打印文章集合而没有关系。
最佳答案
添加@Deadly 发布的内容:
发布复合可以方便地在单个订阅中获取相关文档。这些文档的处理方式仍然与您进行 2 个单独的订阅相同。
在您的情况下,您将有两个客户端集合。一组文章和一组类别。您本地收藏中的哪些文章和哪些类别基于您订阅的内容。
// get a subscription to 'articles'. Use $scope.$meteorCollection so
// the subscription is destroyed when the $scope is destroyed. If you don't you will end up carrying these collections on to anther page.
$scope.$meteorSubscribe('articles').then(function() {
// This swill get you the articles from the local collection
$scope.articles = $scope.$meteorCollection(Articles);
// then you need to get the related Categories for the articles
$scope.getCategories = function(article) {
return $scope.$meteorObject(Categoris, article._id);
}
});
关于angularjs - 将发布复合与 Angular-Meteor 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33014849/