Hoping someone can help, I’m stuck on how to filter a collection for display based on user selections.
For example:
Say you have a list of recipes and they are grouped by ‘meat’ or ‘veg’. In the template, there is buttons for ‘Meat’ and ‘Veg’ and below the buttons is a drop-down with a list of recipes based on which button has been selected.
How do I set that up? I’m a bit lost with adding the interaction on the page as opposed to publishing/subscribing to the data.
Personally for something like this I would say to use your router for this… Creating a route definition as something like /recipes/:group would be ideal. Then when you navigate to /recipies/meat you subscribe to only the necessary data (setting up pagination or infinite scroll if needed) and you render your page with only the group specified.
Please don’t subscribe to every recipe record you have… First this well be taxing on your server and second it will be slow to load on the client… It will work ok if you only have about 100 records or so, but for an optimal response and render time, I wouldn’t recommend subscribing/rendering more than about 30 at a time. That being said you’ll likely want to paginate. Storing the current page in the query of the url is a very good place and if you’re using Iron Router it will provide you with a reactive variable to which you can respond to the changes of.
I would agree with you however I probably need to elaborate on my context. The recipe selection is part of a larger form so I’m interested in being able to filter whilst already having delivered the template.
Totally agree on not subscribing to every record, the available recipes are already paired down on subscription.
In which case I would say that storing the group in the query of the url is a good option for filtering that as well, that way if the user navigates away and then back the state of the form is preserved and they won’t have to redo the form… You could accomplish it through reactive vars on the template instance but I think query of the url is the cleanest solution.