2017-06-03 216 views
0

此問題與此相關post結合流星彙集

總之,我試圖建立一個新聞源,就像Facebook的無限滾動功能。在這份新聞推送中,我想根據發佈的日期顯示來自不同收藏的帖子。我有一個部分工作的代碼(檢查如下),,但問題是,因爲我已將limit設置爲3,刷新後,網站將從每個集合投放3個帖子,而不是從任何3個最新帖子集合。

我認爲這是因爲我發佈集合的方式,或者我訂閱它們的方式。下面是代碼:

publish.js

Meteor.publish('newsfeed', function(limit){ 
    data = [ 
    Status.find({}, {limit:limit, sort: {createdAt:-1}}), 
    Story.find({}, {limit:limit, sort: {createdAt:-1}}), 
    Lesson.find({}, {limit:limit, sort: {createdAt:-1}}), 
    Images.find(), 
    Documents.find() 
    ]; 
    return data; 
}); 

statusBox.js

newsfeed_increment = 3; 
Session.setDefault('newsfeedLimit', newsfeed_increment); 
Deps.autorun(function(){ 
    Meteor.subscribe('newsfeed', Session.get('newsfeedLimit')); 
}); 

Template.statusBox.helpers({ 
    //Merging few collections into one template helper: https://stackoverflow.com/questions/21296712/merging-collections-in-meteor 
    newsfeedList: function(){ 
    var hza = Status.find().fetch() 
     .concat(Story.find().fetch()) 
     .concat(Lesson.find().fetch()); 
     var sortie = _.sortBy(hza, function(doc) { return doc.createdAt; }); 
     var ietros = sortie.reverse(); 
     return ietros; 
... 
}); 

任何想法?

回答

0

你應該進行排序,並在客戶端做其他過濾器爲好,即,在

statusBox.js

Template.statusBox.helpers({ 
    newsfeedList: function(){ 
    var limit = Session.get('newsfeedLimit'); 
    var hza = Status.find({limit:limit, sort: {createdAt:-1}).fetch() 
     .concat(Story.find({limit:limit, sort: {createdAt:-1}).fetch()) 
     .concat(Lesson.find({limit:limit, sort: {createdAt:-1}).fetch()); 
    var sortie = _.sortBy(hza, function(doc) { return doc.createdAt; }); 
    var ietros = sortie.reverse(); 
    return ietros; 
    ... 
});