【问题标题】:Meteor reactive publish流星反应发布
【发布时间】:2016-03-24 13:37:59
【问题描述】:

此 Meteor 代码在模板上的 headerLabel 上显示一条消息,服务器和/或客户端通过在 HeaderLabelCol mongo 集合中插入一条新消息来更改消息,并期望客户端模板在发布最后插入的文档。

我能够使用客户端浏览器插入一条新消息,但直到我刷新页面后才显示,这可能表明反应链在某处被破坏。问题是什么?如何修复?谢谢

//client.js
Template.header.helpers({
  headerLabel: function () {
    return HeaderLabelCol.findOne() ? HeaderLabelCol.findOne().headerLabel : 'Make a selection';
  }
});

//server.js
HeaderLabelCol = new Mongo.Collection('headerLabelCol');
Meteor.publish('headerLabelCol', function () {
  return HeaderLabelCol.find({userId: this.userId}, { sort: { createdAt: -1 } });
});

HeaderLabelCol._ensureIndex({createdAt: -1});
HeaderLabelCol.before.insert(function (userId, doc) {
  doc.userId = userId;
  doc.createdAt = Date.now();
});
HeaderLabelCol.allow({
  insert: function (userId, doc) {
    return (userId && doc.owner === userId);
  }
});

【问题讨论】:

    标签: meteor


    【解决方案1】:

    我认为您还需要在帮助程序中添加条件。

    //client.js
    Template.header.helpers({
        headerLabel: function () {
            var result = HeaderLabelCol.findOne({}, { sort: { createdAt: -1 } });
            return result ? result.headerLabel : 'Make a selection';
        }
    });
    

    【讨论】:

    • 为什么要在客户端排序,因为服务器已经对索引进行了排序,我希望我的 previous 帖子可以正确完成
    • @FredJ。在许多现实世界的大型应用程序中,可以有许多不同的订阅在同一页面中返回集合的数据。可以说,一个模板有 2 个子模板。 template1 订阅 pub1template2 订阅 pub2。如果pub1pub2 都从collection1 返回数据并使用不同的属性进行排序(一个按createdAt,另一个按_id,那么minimongo 将在客户端混合结果。所以它不能总是确保在客户端维护服务器排序。因此您需要像在服务器上一样再次进行过滤和排序。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-24
    • 2014-05-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多