如何隔离订阅 数据 </跨度> 模板之间?
例如 - 我有一个页面有两个不同的模板:
1)主题列表
2)热门话题。
我有两个不同的Meteor.publish和 Topic.find({},{sort:{createdAt:-1}}); } });
2)在热门列表中我排序 数据 </跨度> 按评级字段。
Meteor.subscribe( ‘popularTopics’); Template.top_topics.helpers({ 主题:功能
这是Meteor当前出版物模型的已知限制。
想到的一些选项可以克服这个问题:
该 阿波罗堆栈 也可能很方便。
observeChanges 可能是你需要的。它允许您将文档发布到特定集合,因此您可以拥有两个出版物( topics 和 popularTopics )从服务器上的相同集合中获取数据( Topics ),但将其发送到客户端上的不同集合(例如, Topics 和 PopularTopics )。
topics
popularTopics
Topics
PopularTopics
这是一个例子:
// globally somewhere const Topics = new Mongo.Collection('topics'); const PopularTopics = new Mongo.Collection('populartopics');
添加您的出版物,observeChanges将发布的文档发送到客户端上的两个不同集合:
// topics.publications.js const abstractPublish = function (collectionName, query) { const cursor = Topics.find(query); const cursorHandle = cursor.observeChanges({ added(id, fields) { this.added(collectionName, id, fields); }, changed(id, fields) { this.changed(collectionName, id, fields); }, removed(id) { this.removed(collectionName, id); } }); this.onStop(()=>{ if (cursorHandle) cursorHandle.stop(); }); this.ready(); }; Meteor.publish('topics', function () { // set up a publication to the "topics" collection abstractPublish.call(this, 'topics', {}); }); Meteor.publish('popularTopics', function () { // set up a publication to the "populartopics" collection abstractPublish.call(this, 'populartopics', {popular: true}); });
然后设置模板级订阅:
// topics_main.js Template.topics_main.onCreated(function () { this.autorun(() => { this.subscribe('topics', function () { Topics.find().fetch(); // returns all topics ); }); });
在您热门的主题模板中:
// top_topics.js Template.top_topics.onCreated(function () { this.autorun(() => { this.subscribe('popularTopics', function () { PopularTopics.find().fetch(); // returns only topics that have {popular: true} ); }); });