Page Contents

The LoopBack app object is a Node EventEmitter, and thus has app.emit() and app.on() methods.

In addition to the standard Node events, LoopBack applications and models emit other events.

Application events

By default, an application scaffolded with apic loopback emits a ‘started’ event when it starts up, after running boot scripts.

Model events

All models emit the following events:

By default, the basic LoopBack Model object has properties and methods “mixed in” from:

When you define relations between models, the RelationMixin object object also gets mixed in to the model object.

changed

Emitted after a model has been successfully created, saved, or updated. Argument: inst, model instance, object.

For example:

MyModel.on('changed', function(inst) {
  console.log('model with id %s has been changed', inst.id);
  // => model with id 1 has been changed
});

deleted

Emitted after an individual model has been deleted. Argument: id, model ID (number).

For example:

MyModel.on('deleted', function(id) {
  console.log('model with id %s has been deleted', id);
  // => model with id 1 has been deleted
});

deletedAll

Emitted after an individual model has been deleted. Argument: where (optional), where filter, JSON object.

For example:

MyModel.on('deletedAll', function(where) {
  if (where) {
    console.log('all models where ', where, ' have been deleted');
    // => all models where
    // => {price: {gt: 100}}
    // => have been deleted
  }
});

attached

Emitted after a Model has been attached to an app.

dataSourceAttached

Emitted after a Model has been attached to a DataSource.

set

Emitted when model property is set. Argument: inst, model instance, object.

For example:

MyModel.on('set', function(inst) {
  console.log('model with id %s has been changed', inst.id);
  // => model with id 1 has been changed
});

Arguments: data, an object.

PersistedModel events

PersistedModels also have a changed event that listens for model changes.

For example:

MyPersistedModel.on('changed', function(obj) {
   console.log(obj) // => the changed model
});

User model events

The User model User.resetPassword() method emits the ’resetPasswordRequest’ event.