Fork me on GitHub

getCollectionReactively

This method is used to get a context collection variable and watch it reactively, so changes in this collection causes the dependents (autorun, helper functions and subscriptions) to run again.

Please note that getCollectionReactively will not deep watch the collection. That means that if you have a collection of objects and you change a property of one of them, it will not be detected as a change. For a deep watch please use getReactively and set isDeep argument to true.

This method is primarily used to detect collection length changes: when you change the collection itself, like adding or removing elements. For these scenarios it has a performance boost over getReactively.

The getCollectionReactively method is part of the ReactiveContext and available on every context and $scope. It's bound to the context since the argument for it is the name of a collection that available on the context. When the context is destroyed, the watcher will be stopped.


Arguments

Name Type Details Required
collectionName String The name of the context's collection variable to bind to Yes

Return value

This method returns the current value of the reactive collection variable.


Example with subscribe:

myModule.controller('MyCtrl', ['$scope', '$reactive', function($scope, $reactive) {
  $reactive(this).attach($scope);

  this.relevantIds = [10,11,12];

  this.subscribe('users', () => {
    return [ this.getCollectionReactively('relevantIds') ];
  });

  this.relevantIds.push(13); // This will cause the subscribe arguments method to run again
}]);

Example with helpers:

myModule.controller('MyCtrl', ['$scope', '$reactive', function($scope, $reactive) {
  $reactive(this).attach($scope);

  this.myUser: {
    messages: ['message1', 'message2']
  };

  this.helpers({
    myData: () => {
      return Data.find({_id: this.getCollectionReactively('myUser.messages')});
    }
  });

  this.myUser.messages.push('message3'); // This will cause the helper to run again and update the view
}]);