我想加载一个主题,它的 25 个评论和每个评论最多 5 个子评论,在每个评论/子评论上递归重复,直到找到所有相关评论。
我目前正在使用角度指令递归订阅并添加到本地集合,只要评论有孩子。它工作得很好,但是在加载最初的 25 条评论和加载他们的孩子,然后是他们的孩子等等之间有一些滞后(可以预料)。
当一次只加载一个页面时,这个问题不是问题。当使用无限滚动并增加最初的 25 条评论限制时,这会成为一个问题。当子评论消失并再次加载后重新出现时,它将导致页面上下跳跃。
我想知道如何在发送给本地客户之前递归查找所有评论,这样我就不需要为每个主题进行多次往返。
如果滚动到底部,它会加载更多,并且随着子评论重新加载到页面中,您会看到它跳跃。
我能想到的两个选项是在加载页面之前使用解析来等待所有集合,或者使用递归发布来首先获取所有集合。
想法?想法?
好的,我的第一次尝试,如果可能的话,我想要一些想法。
对于发布,我决定使用publish-composite以使同一集合的发布更容易。
对于我写的出版物:
Meteor.publishComposite('oneDiscussion', function (slug, options) {
var query = {};
query.find = function () {
return Discussions.find({ slug: slug }, { limit: 1 });
};
var mainChildQuery = Comments.find({ slug: slug }, { limit: 1 });
query.children = [];
query.children[0] = {};
query.children[0].find = function (discussion) {
return mainChildQuery;
};
query.children[0].children = [];
query.children[0].children[0] = {};
query.children[0].children[0].find = function (comment) {
return Meteor.users.find({ _id: comment.author.id }, { limit: 1, fields: { profile: 1, roles: 1, createdAt: 1, username: 1 } });
};
query.children[0].children[1] = {};
query.children[0].children[1].find = function (parent) {
Counts.publish(this, 'numberOfComments', Comments.find(
{ parent_id: parent._id }
), { noReady: true });
console.log(options)
return Comments.find({ parent_id: parent._id }, options);
};
// var parentQuery = Comments.find({ slug: slug });
var parent = mainChildQuery.fetch();
var children = Comments.find({ parent_id: parent[0]._id }, { limit: 25 }).fetch();
var childrenIds = _.pluck(children, '_id');
var getChildren = function (children_ids, thisParent) {
var i = 0;
thisParent.children = [];
var recursive = function getEm(children, parent) {
_.each(children, function (id) {
// parent.children[i] = new Children(id);
var query = Comments.find({ parent_id: id }, { limit: 5, sort: { date: -1 } });
parent.children[i] = {
find: function () {
return Comments.find({ parent_id: id }, { limit: 5, sort: { date: -1 } });
}
};
var children1 = query.fetch();
var newChildrenIds = _.pluck(children1, '_id');
i++;
if (newChildrenIds.length > 0) {
getEm(newChildrenIds, parent);
}
});
}
recursive(children_ids, thisParent);
};
getChildren(childrenIds, query.children[0].children[1]);
return query;
});
到目前为止似乎工作正常,虽然在我的桌面上运行它并没有我想象的那么好。我会部署它,看看在线是否有区别。我回家后会更新,并且可以更新实时站点。如果有人能发现我写的东西有问题,将不胜感激。