Add Operation Hooks to a LoopBack model

InstructorBram Borggreve

Share this video with your friends

Send Tweet

In this lesson we will learn how to add operation hooks to our models. In the Product model we will create a before safe observer that will check if the category we want to add the product to exists.

In our Category model we will create a before delete observer to prevent categories from being deleted when they have products.

We will create a unit test to verify that both of these operation hooks work as expected.

Victor Hazbun
~ 7 years ago

why the product hook returns next() and the category model does not?

Frederic Rey
~ 6 years ago

Hello! It seems we can do the same with an Async Validator, is there an advantage using a Hook over it ? What I can see is:

  • With a Validator, only the current Model Instance is available, unless the Model itself is in a parent scope (which is the case) or injected
  • With a Hook, at least the current Model instance and every Model are available