【问题标题】:How can i publish the same collection under different names in a Meteor app?如何在 Meteor 应用程序中以不同的名称发布相同的集合?
【发布时间】:2012-08-28 04:31:50
【问题描述】:

在我的应用程序中,我有一个视频列表的集合,它仅包含经过身份验证的用户的视频,并且希望发布相同的集合以包含来自所有用户的最新 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}); 
});

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

提前致谢

【问题讨论】:

    标签: javascript meteor


    【解决方案1】:

    当您在 Meteor.publish(..) 中返回光标时,Meteor livedata 包对收藏和订阅有一些魔力。

    Tom Coleman 举了一个很好的例子来说明如何弯曲 Meteor 来做你正在寻找的东西:In Meteor how can I publish one server side mongo collection under different names?

    基本上你应该按照他的建议去做:-

    • 调用内部_publishCursor 函数,传入PlayLists.find({}) 光标和latestlists 作为订阅名称。

    -或者-

    • 复制_publishCursor 函数并将其放入一个包中以便可重用。

    这两种方法都行得通,我更喜欢后者,因为我总是对调用内部函数持谨慎态度,因为它们很可能会在你下面发生变化。

    【讨论】:

      猜你喜欢
      • 2012-07-29
      • 1970-01-01
      • 1970-01-01
      • 2015-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多