$meteor.subscribe / $scope.$meteorSubscribe
has been deprecated in favor of the new subscribe syntax.
A service which is a wrapper for Meteor.subscribe. It subscribes to a Meteor.publish method in the client and returns a AngularJS promise when ready.
Calling $scope.$meteorSubscribe will automatically stop the subscription when the scope is destroyed.
$meteor.subscribe(name, publisherArguments)
$scope.$meteorSubscribe(name, publisherArguments)
This would require the angular-meteor-promiser package to work.
$promiser.subscribe(name, publisherArguments)
Param | Type | Details | Required |
---|---|---|---|
name | string | Name of the subscription. Matches the name of the server's publish() call. |
Yes |
publisherArguments | any | Optional arguments passed to publisher function on server. |
No |
onStop | Object with onStop callback | The onStop callback is called after the returned promise is resolved. It will be called with a Meteor.Error if the subscription fails or is terminated by the server. If the subscription is stopped by calling stop on the subscription handle or inside the publication, onStop is called with no arguments. |
No |
promise(subscriptionHandle) | The promise solved successfully when subscription is ready. The success promise holds the subscription handle. |
//Define new Meteor Mongo Collections
Todos = new Mongo.Collection('todos');
Books = new Mongo.Collection('books');
if (Meteor.isClient) {
app.controller("mainCtrl", ['$scope', '$meteor',
function($scope, $meteor){
$meteor.subscribe('todos').then(function(subscriptionHandle){
// Bind all the todos to $scope.todos
$scope.todos = $meteor.collection(Todos);
console.log($scope.todos + ' is ready');
// You can use the subscription handle to stop the subscription if you want
subscriptionHandle.stop();
});
$scope.$meteorSubscribe('books', {onStop: notifyUser}).then(function(subscriptionHandle){
// Bind all the books to $scope.books
$scope.books = $meteor.collection(Books);
console.log($scope.books + ' is ready');
// No need to stop the subscription, it will automatically close on scope destroy
});
////////////
function notifyUser(err) {
if (err)
alert(err.reason);
else
alert('Books subscription was stopped.');
}
}
]);
}
if (Meteor.isServer) {
Meteor.publish('todos', function(){
return Todos.find({});
});
Meteor.publish('books', function(){
return Books.find({});
});
}