Reputation: 4848
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:
Upvotes: 2
Views: 390
Reputation: 2848
There's a a number of differences. Here's just a couple worth noting:
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
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.
Upvotes: 1