我有两个集合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控制器是
angular.module("dee").controller("ArticlesListCtrl", ['$scope', '$meteor', function($scope, $meteor){
$scope.articles = $meteor.collection(Articles).subscribe('articles');
}]);
并且视图是
{{ articles | json }}
问题是它仅打印没有关系的文章集。
最佳答案
添加到@Deadly发布的内容:
通过发布复合文件,可以方便地在单个订阅中获取相关文档。处理这些文档的方式与您进行2次单独订阅的方式相同。
在您的情况下,您将有两个collections客户端。一种是“文章”集合,另一种是“类别”集合。本地收藏集中的哪些文章和哪些类别基于您进行的订阅。
// 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使用publish-composite,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33014849/