我正在使用流星,并与withTracker反应以获取动态数据。我有大量数据,我必须在服务器端过滤数据,否则,客户端崩溃。为此,我将过滤器用作发布的参数。
问题是当我更改查询的一个字段(在过去的请求中)时,组件不会更新。为了解决这个问题,我添加或删除了{“ _id”:{$ exists:true}},以使组件刷新而不使用缓存数据。
服务器端发布:
Meteor.publish('subredOrders', function(filters={}, sort={"_id":1}, pagination={skip:0,limit:2000}, fields={}) {
return SubredOrders.find(filters, {sort: sort, skip: pagination.skip, limit: pagination.limit, fields: fields});
});
Meteor.publish('subredOrders.count', function(filters={}) {
Counts.publish(this, 'subredOrders.count', SubredOrders.find(filters), {fastCount: true});
});
客户端组件:
class MyTable extends Component {
constructor(props) {
super(props);
this.state = {
filtersPanel: false,
};
this.temp = {filters: this.props.params.filters};
}
toggleFiltersPanel(){
let filtersPanel = this.state.filtersPanel;
filtersPanel = !filtersPanel;
*if (!filtersPanel) {
if (this.temp.filters["$and"] && this.temp.filters["$and"].length > 0) {
let removed = false;
for(let i=0; i < this.temp.filters["$and"].length; i++){
for(let key in this.temp.filters["$and"][i]) {
if (key === "_id") {
this.temp.filters["$and"].splice(i, 1);
removed = true;
}
}
}
if (!removed) this.temp.filters["$and"].push({"_id":{$exists:true}});
}
this.props.updateParams("filters", this.temp.filters);
}*
this.setState({filtersPanel: filtersPanel});
}
...
render() {
...
}
}
export default withTracker((props) => {
if (props.collector == null) return {};
let sub = Meteor.subscribe(props.subscription, props.params.filters, props.params.sort, props.params.pagination);
let countSub = Meteor.subscribe(props.subscription+".count", props.params.filters);
return {
collection: props.collector.find({}, {limit: props.params.pagination.limit}).fetch(),
dataLoaded: sub.ready() && countSub.ready(),
}
})(MyTable);
我的解决方案在2 *之间。当用户关闭filtersPanel时,它将启动toggleFiltersPanel并检查请求的过滤器部分是否具有_id,并推送或删除以使其与过去的请求不同。如果用户一次更改2次字段,则请求仅在第一次时有效。这就是为什么我必须这样做,但是我不明白为什么...
这不是一个优化的解决方案,也许有人有其他想法,或者至少可以向我解释为什么我必须这样做才能使它起作用?
谢谢!