helpers

The helpers method is part of the ReactiveContext, and available on every context and $scope.

These method are defined as Object, where each key is the name of the variable that will be available on the context we run, and each value is a function with a return value.

Under the hood, each helper starts a new Tracker.autorun. When its reactive dependencies change, the helper is rerun.

To trigger a rerun every time an specific Angular variable change, use getReactively to make your Angular variable reactive inside the helper its used in.

Each helper function should return a MongoDB Cursor and the helpers will expose it as a normal array to the context.


Arguments

Name Type Details Required
definitions Object
Helper Name => Function
Object containing name => function definition, where each name is a string and each function is the helper function. Should return a MongoDB Cursor Yes

Return value

This method returns this, which the the reactive context, in order to provide the ability to chain the logic.


Usage Example:

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

  this.helpers({
    users: () => Users.find({}),
  });
}]);

The example above will create an array on this context, with all the values that available from the Cursor that returned from find method,

Example with getReactively:

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

  this.relevantId = 10;

  this.helpers({
    users: () => Users.find({ _id: this.getReactively('relevantId') })
  });
}]);

In the following example, each change in relevantId variable, will trigger the helpers method again, and the objects returned in the cursor will be different.