Meteor.user()。profile.myProp对所有个人资料孩子都有反应

时间:2016-07-28 03:01:02

标签: meteor

Meteor.user().profile.propA更改时,此Meteor客户端公共方法需要重新运行,但它也会在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;

1 个答案:

答案 0 :(得分:0)

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内获取该数据。