2

I have two Collections Articles and Categories let's say their documents are like this

Article

{
   "_id": "blabla",
   "title" : "title",
   "description" : "description",
   "categoryId" : "catId"
}

Category

{
   "_id": "catId",
   "title" : "category",
   "description" : "description"
}

I want to make a subscription to make them like this

{
   "_id": "blabla",
   "title" : "title",
   "description" : "description",
   "category" : {
       "title" : "category",
       "description" : "description"
   }
}

I tried using publish-composite and here it's my code. Server

Meteor.publishComposite('articles', {
    find: function() {
        return Articles.find({}, { sort: {}, limit: 10 });
    },
    children: [
        {
            find: function(article) {
                return Categories.findOne({ _id: article.categoryId });
            }
        }
    ]
});

And the client angularjs Controller is

angular.module("dee").controller("ArticlesListCtrl", ['$scope', '$meteor', function($scope, $meteor){
    $scope.articles = $meteor.collection(Articles).subscribe('articles');
}]);

and the view is

{{ articles | json }}

the problem is it prints the article collection only without the relation.

4

2 回答 2

3

添加到@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);
    }
});
于 2015-10-09T13:23:45.330 回答
2

控制器:

   angular.module("dee").controller("ArticlesListCtrl", ['$scope', '$meteor', function($scope, $meteor){
        $scope.articles = $scope.$meteorCollection(Articles).subscribe('articles');
        $scope.getCategory = function(article) {
            return $scope.$meteorObject(Categories, article._id);
        };
    }]);

HTML:

<div ng-repeat="article in articles" ng-init="category=getCategory(article)"></div>

我也知道更好的方法,但它不适用于 angular 并且看起来没有人关心它https://github.com/Urigo/angular-meteor/issues/720

于 2015-10-08T15:52:24.903 回答