Fork me on GitHub
This API is deprecated since version 1.3.0 and no longer available since version 1.3.1

$meteor.collection / $scope.$meteorCollection

A service that wraps the Meteor collections to enable reactivity within Angular.

This service binds between the Angular $scope variables and the meteor collection.


This module has been deprecated in favor of the new helpers API.

There is no need for $meteor.collection anymore as with the helpers function we can use regular Mongo.Collection directly without any wrappers. Helpers will make sure to update Angular. We also removed autobind because it's a bad practice and we gain much better performance and easier maintainability both for the library and the apps developed with it.

Here is an example for how to migrate:

Old code:

$scope.parties = $scope.$meteorCollection(function(){
  Parties.find({}, {sort: {createdAt: -1}})
});

$scope.users = $scope.$meteorCollection(function(){
  Meteor.users.find({})
});

New Code:

$scope.helpers({
  parties() {
    return Parties.find({}, {sort: {createdAt: -1}});
  },
  users() {
    return Meteor.users.find({});
  }
});

Service binding include 3 parts:

  • watching the changes in Angular $scope and updating the Meteor Collection. (called "autobind")
  • watching the changes in the Meteor Collection and updating the Angular $scope variables (called "observe")

The autobinding is optional, and can be set to false, so changes in the Angular $scope variables will have to be explicitly saved to the collection to be updated to the Meteor Collection.

The AngularMeteor collection can be explicitly stopped by calling the stop() function. It is important to stop the collection to avoid the increase in number of watchers.

Calling $scope.$meteorCollection is exactly the same as calling $meteor.collection but additionally it will automatically stop the collection when the scope is destroyed.

Therefore using $scope.$meteorCollection is recommended use over $meteor.collection.


Usage

$meteor.collection(collection, autobind)

$scope.$meteorCollection(collection, autobind)

Arguments

Param Type Details Required Default
collection Meteor Collection Object/ Reactive Function

A Meteor Collection or a reactive function to bind to. reactive function can be used with $scope.getReactively to add $scope variable as reactive variable to the cursor.

Yes
autoClientSave Boolean

By default, changes in the Angular collection will automatically update the Meteor collection. However if set to false, changes in the client won't be automatically propagated back to the Meteor collection.

No True
updateCollection Meteor Collection Object

A collection object which will be used for updates (insert, update, delete).

No

Returns

object

Newly created AngularMeteorCollection object.


Example

// Define a new Meteor Mongo Collection
Todos = new Mongo.Collection('todos');

if (Meteor.isClient) {

  app.controller("mainCtrl", ['$scope', '$meteor',
    function($scope, $meteor){

      // Bind all the todos to $scope.todos
      $scope.todos = $meteor.collection(Todos);

      $scope.sticky = true;
      // Bind all sticky todos to $scope.stickyTodos
      // Binds the query to $scope.sticky so that if it changes,
      // Meteor will re-run the query and bind it to $scope.stickyTodos
      $scope.stickyTodos = $meteor.collection(function(){
        return Todos.find({sticky: $scope.getReactively('sticky')});
      });

      // Bind without auto-save all todos to $scope.notAutoTodos
      $scope.notAutoTodos = $scope.$meteorCollection(Todos, false).subscribe("publicTodos");

      // todo might be an object like this {text: "Learn Angular", sticky: false}
      // or an array like this:
      // [{text: "Learn Angular", sticky: false}, {text: "Hello World", sticky: true}]

      $scope.save = function(todo) {
        $scope.notAutoTodos.save(todo);
      };

      $scope.saveAll = function() {
        $scope.notAutoTodos.save();
      };

      $scope.autoSave = function(todo) {
        $scope.todos.push(todo);
      };

      // todoId might be an string like this "WhrnEez5yBRgo4yEm"
      // or an array like this ["WhrnEez5yBRgo4yEm","gH6Fa4DXA3XxQjXNS"]
      $scope.remove = function(todoId) {
        $scope.notAutoTodos.remove(todoId);
      };

      $scope.removeAll = function() {
        $scope.notAutoTodos.remove();
      };

      $scope.removeAuto = function(todo) {
        $scope.todos.splice( $scope.todos.indexOf(todo), 1 );
      }

      $scope.toSticky = function(todo) {
        if (angular.isArray(todo)){
          angular.forEach(todo, function(object) {
            object.sticky = true;
        });
        } else {
          todo.sticky = true;
        }

        $scope.stickyTodos.save(todo);
      };
    }
  ]);
}

if (Meteor.isServer) {

  // Returns all objects in the Todos collection with public set to true.
  Meteor.publish('publicTodos', function(){
    return Todos.find({public: true});
  });

  // Returns all objects in the Todos collection with public set to false.
  Meteor.publish('privateTodos', function(){
    return Todos.find({public: false});
  });

}