2
votes

Loopback seems to have overlapping concepts when handling certain points of time in an model's lifecycle:

https://docs.strongloop.com/display/public/LB/Operation+hooks

Applied through Model.observe

vs

https://docs.strongloop.com/display/public/LB/Events#Events-Modelevents

Applied through Model.on

Both seem to have similar ways of handling CRUD events.

What's the difference between these two types of event systems? And when should I use one over another?

UPDATE:

Apparently the overlapping model events have been deprecated in Loopback v3, so only operation hooks should be used:

https://github.com/strongloop/loopback-datasource-juggler/blob/master/3.0-RELEASE-NOTES.md#remove-deprecated-model-hooks-and-model-events

1

1 Answers

1
votes

There's a a number of differences. Here's just a couple worth noting:

  1. Operation hooks can invoke your callback before OR after certain events. For example the beforeSave/afterSave operation hooks vs the changed event which only invokes the callback after a change in the model

  2. There are some events in a model's lifecycle that only operation hooks invoke, e.g. the loaded operation hooks is invoked whenever an instance of the model is loaded via find(), findOne(), count(), etc. There's also some events only only covered by model events, e.g. dataSourceAttached, which is invoked when the model is attached to a datasource.

So there's some overlap, but there's also difference in the lifecycle events they can watch.