当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');
}
});
},
db.users.update({'_id':'123abc'}, {$set: {'profile.propB':'B'}})
触发自动运行。即使反应数据源是Meteor.user().profile.propA;
答案 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
内获取该数据。