Help with Template Level Subscriptions


#1

I’m trying to create a simple filter on a list of shifts. How do I do this? I’m using template level subscriptions as described in Sacha’s blog post on Discover Meteor. Here is my code. It does not work. Note the bottom of the code.

Template.shiftsList.helpers({
	shifts: function() {
		return Template.instance().shifts();
	},
	hasMoreShifts: function () {
		return Template.instance().shifts().count() >= Template.instance().limit.get();
	}
});

Template.shiftsList.onCreated(function() {
	var instance = this;

	instance.loaded = new ReactiveVar(0);
	instance.limit = new ReactiveVar(10);

	instance.autorun(function () {
		var limit = instance.limit.get();
		var subscription = instance.subscribe('shifts', limit);

		if (subscription.ready()) {
			instance.loaded.set(limit);
		}
	});

	instance.shifts = function() {
		return Shifts.find({}, {limit: instance.loaded.get()});
	};
});

Template.shiftsList.events({
	'click .load-more': function(e, instance) {
		e.preventDefault();
		
		var limit = instance.limit.get();
		limit += 10;
		instance.limit.set(limit);
	},
	'submit form': function(e, instance) {
		e.preventDefault();

		var dateFilter = moment($(e.target).find('[name=dateFilter]').val());
		
		instance.shifts = function() {
			return Shifts.find({startTime: dateFilter});
		}
	}
});

#2

At a glance it seems like reassigning the shifts function is not a good idea, perhaps instead dateFilter should also be a ReactiveVar which you set in the event handler, which is passed into the original definition of the function (in the onCreated callback).

Edit: Also it will help to have a bit more info other than “it does not work” :smile: