如何在Meteor应用程序中以不同的名称发布相同的集合?

时间:2012-09-03 12:24:24

标签: javascript meteor

在我的应用程序中,我有一个视频列表集合,它只带来经过身份验证的用户的视频,并希望发布相同的集合以带来最新的5个视频,但来自所有用户。我正在做以下但没有成功:

//CLIENT
PlayLists = new Meteor.Collection('playlists');
LatestLists = new Meteor.Collection("latestlists");

Meteor.autosubscribe(function () {
    Meteor.subscribe('playlists', Session.get('listkey'));
    Meteor.subscribe('latestlists');    
});

Template.latestlist.latest = function(argument) {
    return LatestLists.find({});
};
Template.list.playlist = function(argument) {
    return PlayLists.find({});
};

//SERVER
PlayLists = new Meteor.Collection('playlists');
LatestLists = new Meteor.Collection("latestlists");

Meteor.publish('playlists', function (playlist) {
  return PlayLists.find({}, {user:this.userId()}); 
});
Meteor.publish('latestlists', function(){
  return PlayLists.find({}, {sort:{when:-1}, limit:5}); 
});

当我运行应用程序时,我的最新列表集合总是空的。实现这个目标的最佳途径是什么?

提前致谢

1 个答案:

答案 0 :(得分:1)

Meteor livedata软件包对收藏品有很大的吸引力。在Meteor.publish(..)中返回Cursor时的订阅。

Tom Coleman举了一个很好的例子,说明如何让Meteor在这里做你想要的事情:In Meteor how can I publish one server side mongo collection under different names?

基本上你应该像他建议的那样做: -

  • 将内部_publishCursor函数称为传递PlayLists.find({})光标和latestlists作为您的订阅名称。

-Or-

  • 复制_publishCursor功能并将其放入包中以便重复使用。

这两种方法都有效,我赞成后者,因为我总是对调用内部函数持谨慎态度,因为它们可能会在你下面发生变化。