gpt4 book ai didi

node.js - 在多个订阅的上下文中处理客户端上的地理查询

转载 作者:太空宇宙 更新时间:2023-11-03 22:33:44 24 4
gpt4 key购买 nike

我的应用程序中有两个不同的订阅:

 Meteor.subscribe('collection');

 Meteor.subscribe('filtered-collection',param1,param2);

我想通过不同的模板助手将数据提供给不同的模板,分别为 allResultsfilteredResults

由于 $geoWithin 在客户端不起作用,并且我需要使用它进行过滤,因此我不能仅通过

过滤第一个订阅
filteredResults = Collection.find(selector);` 

因此,我需要单独订阅它。

所以,问题是:如何从各自的订阅中找到结果集并将其传递给助手?

最佳答案

我终于解决了这个问题。但我认为该解决方案并不理想。

在服务器上:

Collection = new Meteor.Collection('collection');

Meteor.publish('collection',function(){
return Collection.find();
});

Meteor.publish('filteredCollection',function(loc, radius){
var selector = {};
if (radius === undefined)
radius = 100;
if (loc !== undefined && !(isNaN(loc[0]) || isNaN(loc[1]))) {
selector.loc = {
$geoWithin: {
$centerSphere: [loc, radius / 6371]
}
};
}


var sub = this,
handle = null;

var handle = Collection.find(selector).observeChanges({
added: function(id, fields) {
sub.added("filteredCollection", id, fields);
},
changed: function(id, fields) {
sub.changed("filteredCollection", id, fields);
},
removed: function(id) {
sub.removed("filteredCollection", id);
}
});

sub.ready();

this.onStop(function() {
handle.stop();
});

});

在客户端:

Collection = new Meteor.Collection('collection');
FilteredCollection = new Meteor.Collection('filteredCollection');


Meteor.subscribe('collection');
Meteor.subscribe('filteredCollection',loc,radius);


Template.collection.helpers({
collection: function(){
return Collection.find();
},
filteredCollection: function(){
return FilteredCollection.find();
}
});

在客户端,CollectionFilteredCollection 是服务器上同一底层集合的两个不同子集。但是,这两个子集在缓存和持久性方面是否相互依赖,(我认为)完全是一个不同的问题。

关于node.js - 在多个订阅的上下文中处理客户端上的地理查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33088998/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com