【问题标题】:Meteor data isolation流星数据隔离
【发布时间】:2016-08-16 21:16:31
【问题描述】:

模板之间如何隔离订阅数据?

例如 - 我有一页有两个不同的模板:

1) 主题列表

2) 热门话题。

为此,我有两个 不同 Meteor.publish 和 Subscritions。

1) 在主题列表模板中,我已按 CreatedAt 字段排序。

Meteor.subscribe('topics');

  Template.topics_main.helpers({
        topics:function(){
            return Topic.find({},{sort: {createdAt: -1}});
        }
  });

2) 在热门列表中,我按评级字段对数据进行排序。

    Meteor.subscribe('popularTopics');
    Template.top_topics.helpers({
          topics:function(){
               return Topic.find({}, {
           sort: {
               views: -1
           },
           limit: 5
       });

   }
});

当我滚动我的主题列表时,我将从热门主题中获取数据。这不好:) 我怎样才能在两个具有不同订阅但只有一种集合类型的模板之间隔离数据?

【问题讨论】:

    标签: meteor meteor-blaze


    【解决方案1】:

    observeChanges 可能是您需要的。它允许您将文档发布到特定集合,因此您可以让您的两个出版物(topicspopularTopics)从服务器(Topics)上的同一集合中获取数据,但将其发送到不同的集合客户端(例如 TopicsPopularTopics)。

    这是一个例子:

    // 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}
        );
      });
    });
    

    【讨论】:

    • 感谢您的精彩回答!在这种情况下,问题是我的 ORM :) 我使用天文学。通过天文学使用您的方法会有点困难:) 但是,这个决定看起来不错:) 真的 Meteor 在模板级别没有解决方案隔离吗?
    • 如果您不能同时看到两个模板(例如,如果它们位于应用程序的不同页面上),那么使用模板级订阅就足够了。因为当你渲染 top_topics 时,Topic 只包含热门主题,那么当该模板被销毁时,订阅就会停止,并且文档会从客户端中删除。然后你可以渲染topics_main,Topic会被所有的文档(流行和不流行)填满。
    • 是的,我知道不同的页面:) 问题是它们存储在一页中。而且,我只是想找到一个正确的方法,没有hax。所以,如果没有 hack,它看起来就没有办法)图像的完整问题:forums.meteor.com/t/how-can-we-isolate-subscribtions-data/28062/…
    【解决方案2】:

    这是 Meteor 当前发布模型的一个已知限制。

    想到的一些解决方案:

    1. 找到一种使用更精细的查询来区分数据的方法。
    2. 以其他方式获取一些数据(例如,使用方法,而不使用 pub/sub 或集合)。
    3. 发布到“虚拟”集合(通过手动监视发布中的光标)。

    Apollo Stack 也可能很方便。

    【讨论】:

      猜你喜欢
      • 2016-01-18
      • 1970-01-01
      • 1970-01-01
      • 2016-11-27
      • 2022-06-16
      • 2017-12-15
      • 1970-01-01
      • 1970-01-01
      • 2015-04-19
      相关资源
      最近更新 更多