我在确定如何使订阅对某个发布查询有反应方面遇到了一些麻烦。
我的出版物如下:
Meteor.publish('allData', function(){ return Data.find({TTL: {$gt: new Date()}}) })如您所见,文档包含TTL字段。 只要生存时间仍然大于当前时间,它就可以发送给客户端,如果没有,则不应该。
订阅:
this.autorun(() => { this.subscribe('allData') })在初始加载时,所有数据都很好,但是每当TTL到期时,除非我重新加载页面,否则文档将保留在客户端上。 无论如何反过来处理这个,使过期的文件从客户端消失?
I'm having a little trouble figuring out how to make a subscription reactive to a certain publication query.
My publication is as follows:
Meteor.publish('allData', function(){ return Data.find({TTL: {$gt: new Date()}}) })As you can see, the documents contain a TTL field. As long as the Time To Live is still greater that the current time, it can be sent to the client, if not, it shouldn't.
the subscription:
this.autorun(() => { this.subscribe('allData') })On the initial load, all data is fine, but whenever the TTL expires, the document remains on the client unless I reload the page. Is there anyway to handle this reactively, making the expired documents disappear from the client?
最满意答案
ReactiveVar和自动运行的组合为我做了诀窍。 这可能是矫枉过正,但它确实有效。
let cutoffTimestamp = new ReactiveVar(); Meteor.setInterval(function() { cutoffTimestamp.set(Date.now() - 1); }, 60000); Meteor.publish("myPub", function() { this.autorun(function() { return myCollection.find({ timestamp: { $gte: cutoffTimestamp.get(); } }); }); });A combination of ReactiveVar and an autorun did the trick for me. It's possible that this is overkill, but it works.
let cutoffTimestamp = new ReactiveVar(); Meteor.setInterval(function() { cutoffTimestamp.set(Date.now() - 1); }, 60000); Meteor.publish("myPub", function() { this.autorun(function() { return myCollection.find({ timestamp: { $gte: cutoffTimestamp.get(); } }); }); });更多推荐
发布评论