【问题标题】:Meteor.user().profile.myProp reactive for all profile childrenMeteor.user().profile.myProp 对所有配置文件子项反应
【发布时间】:2016-07-28 20:42:25
【问题描述】:

这个 Meteor 客户端公共方法需要在 Meteor.user().profile.propA 更改时重新运行,这很好,但是当 profile.propB 更改或添加时它也会运行。当profile任何其他 子属性已更改或添加但仅适用于profile.propA 时,如何阻止它重新运行?谢谢

    myListener: () => {
      Tracker.autorun(() => {
        if (Meteor.userId()) {
          const indexes = Meteor.user().profile.propA;
          if (!indexes || indexes.length <= 0) return;
          dict.set('myStuff', indexes);
          console.log('auto has run');
        }
      });
    },

在 mongodb 终端上:

db.users.update({'_id':'123abc'}, {$set: {'profile.propB':'B'}})

触发自动运行。即使响应式数据源是Meteor.user().profile.propA;

【问题讨论】:

    标签: meteor


    【解决方案1】:

    Mongo.Collection.findOne 允许您使用fields 选项指定从本地数据库中检索哪些字段。只有更改指定的字段才会再次触发自动运行。

    由于Meteor.user() 只是Meteor.users.findOne(Meteor.userId()) 的简写,您可以执行以下操作以仅获取propA 的更新:

    const indexes = Meteor.users.findOne(Meteor.userId(), {
      fields: {
        'profile.propA': 1
      }
    });
    

    注意indexes 将只包含profile.propA 和文档的_id。如果您需要来自用户文档的更多数据,但仍想单独接收响应式更新,则必须在第二个autorun 中获取该数据。

    【讨论】:

    • 感谢您的指正。我没有意识到跟踪器足够聪明,可以深入到现场级别。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-18
    • 2017-10-14
    • 2019-04-27
    • 2019-03-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多