angularjs - 将发布复合与 Angular-Meteor 结合使用

标签 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 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/

相关文章:

javascript - meteor :设计模式:为集合的每个字段(列)创建模板助手

angular-meteor - 模板冲突角 meteor 1.2

javascript - npm 错误! 404 未找到 npm 错误!不好 代码 0

mongodb - 由于 mongo 退出,无法启动 meteor 应用程序

angularjs - ionic - [ui.router] 无法导航嵌套状态

javascript - 根据当前过滤器过滤源数组,并将其某些值放入 ng-repeat 项

java - 在网络浏览器中运行 Angular 应用程序时获取 "#"以及 URL

javascript - 收到推送通知时如何更改范围变量

meteor - 后台任务未与 Meteor 中的任何客户端连接

jquery - 在 Meteorjs 中使用 Jquery