【问题标题】:Meteor Iron Router, Pub Sub causing weird behaviorMeteor Iron Router,Pub Sub 导致奇怪的行为
【发布时间】:2015-08-21 22:44:26
【问题描述】:

我正在尝试让 sing post page 成为一条使用 Iron:router 做几件事的路线

  1. 使用模板postPage
  2. 订阅singlePostuserStatus(显示单个帖子页面作者的状态和信息)、comments 的发布。
  3. 抓取Comments 具有postId : this.params._id 字段的文档
  4. 增加Session.get('commentLimit')的评论列表

这是我目前拥有的代码。


Router.js

Router.route('/posts/:_id', {
  name: 'postPage',
  subscriptions: function() {
    return [
      Meteor.subscribe('singlePost', this.params._id),
      Meteor.subscribe('userStatus'),
      Meteor.subscribe('comments', {
        limit: Number(Session.get('commentLimit'))
      })
    ];
  },
  data: function() {
    return Posts.findOne({_id:this.params._id});
   },
});

Publications.js

Meteor.publish('singlePost', function(id) {
  check(id, String);
  return Posts.find(id);
});

Meteor.publish('comments', function(options) {
  check(options, {
    limit: Number
  });
  return Comments.find({}, options);
});

Template.postPage.onCreated

Template.onCreated( function () {
  Session.set('commentLimit', 4);
});

Template.postPage.helpers

Template.postPage.helpers({
    comments: function () {
      var commentCursor = Number(Session.get('commentLimit'));
      return Comments.find({postId: this._id}, {limit: commentCursor});
    },
});

Template.postPage.events

Template.postPage.events({
    'click a.load-more-comments': function (event) {
      event.preventDefault();
      Session.set('commentLimit', Number(Session.get('commentLimit')) + 4)
    }
});

一切正常,但我发现有一点不一致。 这是我遇到的问题...

  1. 用户进入单个帖子页面并添加评论(一切正常)。
  2. 用户进入不同的单个帖子页面并添加评论(一切正常)。
  3. 问题从这里开始
    • 用户在任何时候都会进入另一个不是单个帖子页面的路径。
  4. 用户返回单个帖子页面
    • cmets 未显示。
    • 新的 cmets 将添加到 DB 中,但仍不会显示
  5. 只有在执行meteor reset 或手动删除 MongoDB 中的所有 cmets 时,此问题才会消失。

有没有更好的方法来编写路由和相关代码以阻止这种奇怪行为的发生?

或者即使有更好的做法。

【问题讨论】:

    标签: javascript mongodb meteor publish-subscribe iron-router


    【解决方案1】:

    您的发布正在发布没有任何 postId 过滤器的 cmets。

    您的助手,按postId 过滤。也许发布的 4 个 cmets 不属于当前打开的帖子?

    您可以尝试更新,您的订阅

    Meteor.subscribe('comments', {
        postId: this.params._id
    }, {
        limit: Number(Session.get('commentLimit'))
    })
    

    和您的出版物

    Meteor.publish('comments', function(filter, options) {
        check(filter, {
            postId: String
        });
        check(options, {
            limit: Number
        });
        return Comments.find(filter, options);
    });
    

    所以只发布相同帖子的 cmets?

    【讨论】:

    • 我的意图是在页面重新导航/刷新/重新加载等时显示 4 个 cmets。使用 Session.set(value+4) 是为了显示更多 cmets。
    • 没问题。我正在使用订阅 GUI,它会从任何帖子中返回任何 cmets...也许有办法可以调整一些代码,以便 Meteor 只为 this.params._idthis._id 发布 cmets?
    • 我选择了您的答案作为正确答案,因为我认为以这种方式构建它以提高可读性和可扩展性是更好的做法!
    【解决方案2】:

    我想通了。我已经更新了以下代码。

    到目前为止,它没有表现出奇怪的行为......

    Publications.js

    Meteor.publish('comments', function(postId, limit) {
      check(postId, String);
      check(limit, Number);
      return Comments.find({postId:postId}, {limit:limit});
    });
    

    Router.js

    Router.route('/posts/:_id', {
      name: 'postPage',
      subscriptions: function () {
        return [
          Meteor.subscribe('singlePost', this.params._id),
          Meteor.subscribe('userStatus'),
          Meteor.subscribe('comments', this.params._id, Number(Session.get('commentLimit')))
        ];
      },
      data: function() {
        return Posts.findOne({_id:this.params._id});
       },
    });
    

    【讨论】:

    • 是的,我已经用类似的东西编辑了答案:)
    猜你喜欢
    • 1970-01-01
    • 2014-08-13
    • 2014-05-07
    • 2013-11-21
    • 1970-01-01
    • 2023-03-12
    • 2014-10-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多