将发布复合与 Angular-Meteor 结合使用
Using publish-composite with Angular-Meteor
我有两个集合 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 }}
问题是它只打印文章集,没有关系。
控制器:
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
添加@Deadly 发布的内容:
Publish composite 可以方便地在单个订阅中获取相关文档。这些文档的处理方式与您进行 2 次单独订阅时的处理方式相同。
在您的情况下,您将有两个 collections 客户端。一个 collection 用于文章,一个 collection 用于类别。您本地 collection 中的哪些文章和哪些类别基于您进行的订阅。
// 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);
}
});
我有两个集合 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 }}
问题是它只打印文章集,没有关系。
控制器:
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
添加@Deadly 发布的内容:
Publish composite 可以方便地在单个订阅中获取相关文档。这些文档的处理方式与您进行 2 次单独订阅时的处理方式相同。
在您的情况下,您将有两个 collections 客户端。一个 collection 用于文章,一个 collection 用于类别。您本地 collection 中的哪些文章和哪些类别基于您进行的订阅。
// 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);
}
});