[英]Subscribe to Meteor.users collection updates
I use react-komposer and React in my app and at one point I change user's profile
property of Meteor.users
collection item that reflects current user. 我在应用程序中使用react-komposer和React,有一次我更改了反映当前用户的Meteor.users
集合项的用户profile
属性。 The problem here is that it doesn't reactively loopback in a call of render
. 这里的问题在于,它在调用render
不会反应性地回送。 Whatever I do to Meteor.users collection, even though I refer to Meteor.user()
within render
function, doesn't re-render the component. 我对Meteor.users集合所做的任何操作,即使我在render
函数中引用了Meteor.user()
,也不会重新渲染该组件。 If I do 如果我做
render() {
console.log('rendered');
/// ...
}
then I can only see "rendered" on the console twice: once the component has been initially rendered and the second time other subscriptions are ready. 那么我只能在控制台上看到两次“渲染”:最初渲染了该组件,第二次准备了其他订阅。
I'd like to subscribe to Meteor.users
collection but I don't know if there's a way to do. 我想订阅Meteor.users
集合,但我不知道有没有办法。
I could definitely use Tracker to react to every Tracker loop but it feels too 1.2 to me. 我绝对可以使用Tracker对每个Tracker循环做出反应,但对我来说感觉太过1.2。 But is it the only way to do? 但这是唯一的方法吗?
Also, I definitely have read other Q&As about Meteor.users
like this one or this one but the idea of wrapping users collection into pub/sub directly feels unnatural, and hardly is a good way to listen to every update in the collection. 另外,我肯定也读过其他有关Meteor.users
&A的问题, Meteor.users
像这样一个或这个用户,将用户集合包装到pub / sub中的想法直接感觉很不自然,并且几乎不是监听集合中每个更新的好方法。
You have to make a publication on the server code regarding the meteor.users collection. 您必须在服务器代码上发布有关meteor.users集合的发布。
Meteor.publish('allUsers', function allUsers() {
return Meteor.users.find({}, {
fields: {
services: 0,
},
});
});
Then you can subscribe on your container to that publication and query Meteor.users collection and send that result to the component. 然后,您可以在容器上订阅该发布,并查询Meteor.users集合并将该结果发送到组件。
const composer = (props, onData) => {
const userHandle = Meteor.subscribe('allUsers');
if (userHandle.ready()) {
const users = Meteor.users.find({}).fetch();
onData(null, {
users,
});
}
};
export default composeWithTracker(composer)(YourComponent);
After this any change done to the collection inside YourComponent will be reflected client side. 之后,对YourComponent内部集合所做的任何更改都将反映在客户端。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.