After I removed autopublish and set up infinite scrolling (from http://meteorpedia.com/read/Infinite_Scrolling), I have no idea how to get my filters to work how I want because of the limit on the subscribe.
Basically there is a collection of songs, each with a genre. Then there are checkboxes for each genre. Checked boxes are added to an array of selected genres stored in a Session.set which are used to filter in a Songs.find(). That way the user could see songs under multiple selected genres.
The problem is I limited how many items are subscribed to at once, for the sake of speed, so when I click the check box, it only filters among the subscribed items. For example, pretend the collection has 50 songs, 10 rock, 10 hip hop, 30 pop. It is initially subscribed to 10 songs using the code from infinite scrolling. 2 are rock, 2 are hip hop, the rest are pop. If I scroll down it will load more. When I select it to filter rock and hip hop, it will only display the 4 rock/hiphop songs that have already been subscribed.
I would like it to subscribe to another 5 for either category, making it a total of 10 displayed. How can I do this? Iām thinking of using a button that would change the publish/subscribe function to include the filter but I have no idea how that info would flow back to publish or subscribe.
Server:
Meteor.publish('songs', function(limit) {
return Songs.find({}}, {sort: {score: -1}, limit: limit });
});
Iron Router:
this.route('songs', {
path: '/songs/',
subscriptions: function() {
var ITEMS_INCREMENT = 10;
Session.setDefault('SongsLimit', ITEMS_INCREMENT);
Deps.autorun(function() {
Meteor.subscribe('songs', Session.get('SongsLimit'));
});
},
cache: true,
});
Helpers:
Template.songs.helpers({
'song': function(){
if(Session.get('genres') == ""){
return Songs.find({}, {sort: {score: -1}});
} else {
return Songs.find({
"genre": {$in: Session.get('genres')}},
{sort: {score: -1}}
});
}
});
Events:
Template.filtergenre.events({
'click input[class=checkbox-genre]': function (ev, tpl) {
var genres = tpl.$('input:checked').map(function () {
return $(this).val();
});
Session.set('genres', $.makeArray(genres));
}
});
Iām guessing there is some way to include [ āgenreā: {$in: Session.get(āgenresā)} ] into the publish and subscribe but⦠yeah I donāt even know how to begin plugging a filter into publish in general, not to mention using information from the client.
Any advice would be appreciated! Thanks!