Racer model subscriptions - kristianmandrup/racer-mw GitHub Wiki

The subscribe, fetch, unsubscribe, and unfetch methods are used to load and unload data from a model. These methods don’t return data directly. Rather, they load the data into the model. The data are then accessed via model getter methods.

subscribe and fetch both return data initially, but subscribe also registers with PubSub on the server to receive ongoing updates as the data change.

model.subscribe ( items..., callback(err) )

model.fetch ( items..., callback(err) )

model.unsubscribe ( items..., callback(err) )

model.unfetch ( items..., callback(err) )

items: Accepts one or more subscribable items, including a path, scoped model, or query

callback: Calls back once all of the data for each query and document has been loaded or when an error is encountered

Avoid subscribing or fetching queries by document id like model.query('users', {_id: xxx}). You can achieve the same result passing 'users.xxx' or model.at('users.xxx') to subscribe or fetch, and it is much more efficient.

If you only have one argument in your call to subscribe or fetch, you can also call subscribe, fetch, unsubscribe, and unfetch on the query or scoped model directly.

var user = model.at('users.' + userId);
user.subscribe(function(err) {
  if (err) return next(err);
  var todosQuery = model.query('todos', {creatorId: userId});
  todosQuery.subscribe(function(err) {
    if (err) return next(err);
    model.ref('_page.user', user);
    // ...