我的应用程序从Meteor中的服务器获取大量文档。我已经尝试优化Live Query来减少这种情况,但是当collection.find()。count()只有13时,我仍然每小时收到几千个文档。
订阅:
Template.calendar.onCreated( function() {
var self = this;
self.ready = new ReactiveVar();
PostSubs = new SubsManager();
self.autorun(function() {
var handle = PostSubs.subscribe('meals');
self.ready.set(handle.ready());
});
});
公开:
Meteor.publish('meals', function() {
return Meals.find({
userId : this.userId,
start : {
$gte : moment().subtract(2, 'w').format("YYYY-MM-DD"),
$lte : moment().add(2,'w').format("YYYY-MM-DD")
}
});
return this.ready();
});
在上下文中使用:
Template.calendar.onRendered(() => {
$( '#meals-calendar' ).fullCalendar({
defaultView: 'basicWeek',
firstDay: 1,
height: 200,
editable: true,
events( start, end, timezone, callback ) {
let data = Meals.find().fetch().map( ( event ) => {
return event;
});
if ( data ) {
callback( data );
}
},
eventRender( event, element ) {
element.find( '.fc-content' ).html(
`<strong>${ event.title }</strong>`
);
},
dayClick( date ) {
Session.set( 'eventModal', { type: 'add', date: date.format() } );
$( '#add-edit-event-modal' ).modal( 'show' );
$('#title').focus();
},
eventClick( event ) {
Session.set( 'eventModal', { type: 'edit', event: event._id } );
$( '#add-edit-event-modal' ).modal( 'show' );
},
eventDrop( event, delta, revert ) {
let date = event.start.format();
let update = {
_id: event._id,
start: date
};
Meteor.call( 'editEvent', update, ( error ) => {
if ( error ) {
Bert.alert( error.reason, 'danger' );
}
});
},
});
Tracker.autorun( () => {
Meals.find().fetch();
$( '#meals-calendar' ).fullCalendar( 'refetchEvents' );
});
});