重叠流星出版物 [英] Overlapping Meteor publications

查看:23
本文介绍了重叠流星出版物的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个流星应用程序,其中有 2 个用于发布帖子的出版物.一个用于所有帖子,一个用于精选帖子.有 2 个精选帖子 - 帖子 1"和帖子 4".我在所有页面上显示精选帖子,而我按名称对所有帖子(包括精选帖子)进行分页.当我在页面之间移动时,来自 2 个出版物的数据会混淆并显示不正确的结果.

I have a meteor app which has 2 publications for posts. One for all posts and one for featured posts. There are 2 featured posts - "Post 1" and "Post 4". I show featured posts on all pages, while i paginate all posts (including the featured posts) sorted by name. When i travel between pages, the data from the 2 publications get mixed up and show incorrect results.

代码如下:

Meteor.publish('posts', function(page) {
  const skip = parseInt(page && page !== '' ? page : 0) * 3
  return Posts.find({}, {
    limit: 3,
    skip,
    sort: {
      name: 1
    }
  });
});

Meteor.publish('featured', function() {
  return Posts.find({
    featured: true
  }, {
    sort: {
      name: 1
    }
  });
});

在客户端上,我订阅了两者并在 2 个循环中显示数据

On the client I am subscribing to both and displaying the data in 2 loops

Template.hello.onCreated(function helloOnCreated() {
  const instance = this;
  instance.autorun(function() {
    instance.subscribe('posts', FlowRouter.getParam('page'))
    instance.subscribe('featured')
  });
});

Template.hello.helpers({
  posts() {
    return Posts.find({}, {
      limit: 3,
      sort: {
        name: 1
      }
    })
  },
  featured_posts() {
    return Posts.find({
      featured: true
    }, {
      sort: {
        name: 1
      }
    });
  }
});

HTML 模板如下:

<template name="hello">
  <h2>Featured</h2>
  {{#each featured_posts}}
    {{> post}}
  {{/each}}
  <h2>Posts</h2>
  {{#each posts}}
    {{> post}}
  {{/each}}
</template>

问题是来自 2 个订阅的数据在显示中混淆了.

The problem is the data from the 2 subscriptions are getting mixed up in the display.

在第 1 页它正确显示:

On page 1 it shows it correctly:

Page 1

Featured
  post 1
  post 4

All Posts
  post 1
  post 2
  post 3

但是当我转到第 2 页时

but when I go to page 2

Page 2

Featured
  post 1
  post 4

All Posts  -- Should be
  post 1        post 4
  post 4        post 5
  post 5        post 6

它在帖子"中显示了帖子 1",这是特色但不应该出现在第 2 页上.当我转到第 3 页时,我看到帖子 1"和帖子 4",但它们不应该在那里.

It shows "post 1" inside the "posts" which is featured but shouldn't be on page 2. When I go to page 3, i see "post 1" and "post 4" but they should not be there.

我了解发布和订阅的工作原理以及发生这种情况的原因 - 因为发布合并了数据集.我想知道是否有办法将它们分开?

I understand how the publications and subscriptions work and why this is happening - because the publications merges the dataset. I am wondering if there is a work around to keep them separate?

推荐答案

如果我理解正确,您的页面对应于所有帖子"列表的分页.页码"作为您的订阅参数发送,以便您收到帖子的简短列表.

If I understand correctly, your pages correspond to pagination of your "All Posts" list. The "page" number is sent as your subscription parameter, so that you receive a short list of your posts.

这里的困难确实是您的 Client 集合没有您所有的文档(因为您将它们限制在您的 'posts' 出版物中),因此您不能使用类似的 skip 发布中的逻辑.

The difficulty here is indeed that your Client collection does not have all your documents in hand (since you limit them in your 'posts' publication), so you cannot use a similar skip logic as in the publication.

正如 Meteor Guide > 分页订阅中所建议的,您可以使用percolate:find-from-publication 大气包轻松检索来自您的 'posts' 出版物的文档,并且只有它们.

As proposed in Meteor Guide > Paginating subscriptions, you could use the percolate:find-from-publication Atmosphere package to easily retrieve the documents that come from your 'posts' publication, and only them.

// Server
FindFromPublication.publish('posts', function(page) {
  // Same logic
  const skip = parseInt(page && page !== '' ? page : 0) * 3
  return Posts.find({}, {
    limit: 3,
    skip,
    sort: {
      name: 1
    }
  });
});

// Client (no change in subscription)
Template.hello.helpers({
  posts() {
      return Posts.findFromPublication('posts', {}, {
        sort: {
          name: 1
        }
      });
    } // (no change in featured_posts)
});

这篇关于重叠流星出版物的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆