使用带有Angular-Meteor的publish-composite

时间:2015-10-08 11:46:01

标签: angularjs meteor angular-meteor

我有两个收藏ArticlesCategories让他们说他们的文件是这样的

文章

{
   "_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 }}

问题是它只打印文章集合而没有关系。

2 个答案:

答案 0 :(得分: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);
    }
});

答案 1 :(得分:2)

控制器:

#!/bin/sh 

myfunc()
{
    echo kill "$@" $(jobs -p) 
}

myfunc -l -L


myfunc -l

HTML:

   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);
        };
    }]);

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