Iâve been studying publications and subscriptions a bit more.
It looks like subscriptions are smarter when they are put inside a Tracker.autorun.
If you do something like this:
var catalogSub = Meteor.subscribe('Catalog.catalogSearch', "");
var searchProducts = function(search){
catalogSub.stop();
catalogSub = Meteor.subscribe('Catalog.catalogSearch', search);
};
All the data is thrown away and then sent again.
But if you do something like this:
Tracker.autorun(function(){
Meteor.subscribe("Catalog.catalogSearch",Session.get("Catalog.searchQuery"));
});
var searchProducts = function(search){
Session.set("Catalog.searchQuery", search);
};
Meteor diffs the data and sends only what is necessary.
It seems like there is no way to do that without Tracker and a reactive variable.
In the new Meteor 1.0.4 you can set subscriptions in Templates and Meteor creates/destroys them automatically when the Template is created/destroyed. It gives you a helper called Template.subscriptionsReady as well.
Iâve been thinking if it is ok to move subscriptions from Stores to Views in a Flux app.
If Views (Template.helpers) are allowed to decide what to retrieve from Minimongo:
Template.SomeView.helpers({
some_helper: function(){
return SomeCollection.find({name: "something"});
}
});
They should be allowed to decide what to retrieve from MongoDB, shouldnât they?
Template.SomeView.onCreated(function () {
var self = this;
self.autorun(function () {
self.subscribe("someSubscription", Session.get("somethingReactive"));
});
});
I am not 100% sure yet, but I think it may work.
Views are still not allowed to insert/update data in Collections or set Session variables. If a View wants to modify a subscription it has to send an Action and a Store has to change the App State (Session or Collection which affects the subscription). So App State is still managed in Stores.
I would prefer if you donât have to include a reactive variable in the subscription but it looks thereâs no way around it.
I have refactored and updated the repo and the demo:
http://cartflux.meteor.com
By the way, before routers, I am going to:
- Remove insecure package
- Include user accounts
- Implement pagination