不要怂,就是干,撸起袖子干!

Commit 2b756acd by Alexsey Committed by Sushant

docs(hooks): some refinements of hooks declarations (#8260)

1 parent 7e441a6a
Showing with 4 additions and 2 deletions
...@@ -65,12 +65,12 @@ const User = sequelize.define('user', { ...@@ -65,12 +65,12 @@ const User = sequelize.define('user', {
} }
}); });
// Method 2 via the .hook() method // Method 2 via the .hook() method (or it's alias .addHook() method)
User.hook('beforeValidate', (user, options) => { User.hook('beforeValidate', (user, options) => {
user.mood = 'happy'; user.mood = 'happy';
}); });
User.hook('afterValidate', (user, options) => { User.addHook('afterValidate', 'someCustomName', (user, options) => {
return sequelize.Promise.reject(new Error("I'm afraid I can't let you do that!")); return sequelize.Promise.reject(new Error("I'm afraid I can't let you do that!"));
}); });
...@@ -102,6 +102,8 @@ Book.addHook('afterCreate', 'notifyUsers', (book, options) => { ...@@ -102,6 +102,8 @@ Book.addHook('afterCreate', 'notifyUsers', (book, options) => {
Book.removeHook('afterCreate', 'notifyUsers'); Book.removeHook('afterCreate', 'notifyUsers');
``` ```
You can have many hooks with same name. Calling `.removeHook()` will remove all of them.
## Global / universal hooks ## Global / universal hooks
Global hooks are hooks which are run for all models. They can define behaviours that you want for all your models, and are especially useful for plugins. They can be defined in two ways, which have slightly different semantics: Global hooks are hooks which are run for all models. They can define behaviours that you want for all your models, and are especially useful for plugins. They can be defined in two ways, which have slightly different semantics:
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!