Skip to content

Instantly share code, notes, and snippets.

@zeroasterisk
Created January 19, 2016 03:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeroasterisk/69d322fc69a1c201df87 to your computer and use it in GitHub Desktop.
Save zeroasterisk/69d322fc69a1c201df87 to your computer and use it in GitHub Desktop.
possible autoSubscribe function for subscription management linking - re: https://kadira.io/academy/meteor-routing-guide/content/subscriptions-and-data-management/using-subs-manager
// Subscriptions automatic helpers
autoSub = { readys: {}, managers: {} };
autoSubscribe = function(T, subKey, paramKey) {
T.onCreated(function() {
var self = this;
if (!_.has(autoSub.managers, subKey)) {
autoSub.managers[subKey] = new SubsManager();
}
if (!_.has(autoSub.readys, subKey)) {
autoSub.readys[subKey] = new ReactiveVar();
}
self.ready = autoSub.readys[subKey];
self.autorun(function() {
var handle = autoSub.managers[subKey].subscribe(subKey, FlowRouter.getParam(paramKey));
self.ready.set(handle.ready());
});
});
};
Template.registerHelper('autoSubscribeReady', function() {
if (!Template.instance().ready) {
console.log('Template.autoSubscribeReady=false, no ready', Template.instance());
return false;
}
return Template.instance().ready.get();
});
Template.registerHelper('autoSubscribeUser', function() {
if (!Template.instance().ready) {
console.log('Template.autoSubscribeUser=false, no ready', Template.instance());
return false;
}
return Users.findOne({ _id: FlowRouter.getParam('userId') });
});
Meteor.startup(function() {
autoSubscribe(Template.AdminUsersView, 'user', 'userId');
autoSubscribe(Template.AdminUsersEdit, 'user', 'userId');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment