An angular.js service that creates and maintains Meteor.js subscriptions across state transitions.
Declare subscriptions required for your states:
.state('bookshop', {
url: '/books/:bookId/:sort',
templateUrl: ...,
data: {
$subs: [
{ name: 'books', args: ['sort'] },
'favorites'
]
}
});
Configure components that need certain subscriptions to function:
app.directive('commentsList', function($subs) {
return {
controller: function($scope, $element, $attrs, $transclude) {
...
$subs.needsBind($scope, 'comments', $scope.someParam);
}
};
});
app.service('singleton', function($subs) {
var descriptor = $subs.need('something');
...
...
descriptor.stop();
});
Additional features:
$sub
config in your states....
.state('module', {
url: '/module/:moduleId',
parent: 'organisation',
abstract: true,
template: '<ui-view/>',
resolve: {
moduleSub: function($meteor) {
return $meteor.subscribe('modules');
},
... Other resolves
},
onExit: function(moduleSub) {
moduleSub.stop();
}
});
Problems:
onExit
is never called and the subscription is never closed.onExit
isn't necessarily called and completed before the next state transition starts.onExit
called on navigation between child routes? I think they are.function AddressBookController($scope, $rootScope, $state, $modal, $meteor, organisation) {
$scope.$meteorSubscribe('profileImages');
...
Problems:
...
if (scope.type === 'Contact') {
scope.$meteorSubscribe('contacts');
scope.user = scope.$meteorObject(Contacts, scope.userId);
}
...
Problems:
$scope
is unavailable, e.g. in a factory or service, the subscription needs to be stopped manually....
.state('overview', {
...
// Boilerplate
resolve : {
modulesSub: function($meteor) {
return $meteor.subscribe('modules');
}
},
onExit: function(modulesSub) {
modulesSub.stop();
}
})
.state('module', {
...
// Boilerplate
resolve: {
moduleSub: function($meteor) {
return $meteor.subscribe('modules');
},
module: function($meteor, $stateParams, moduleSub) {
return Modules.findOne($stateParams.moduleId);
}
},
onExit: function(moduleSub) {
moduleSub.stop();
}
});
onExit
.Only close / open new subscriptions when required. Consider: