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

Commit af21b157 by Jan Aagaard Meier

Merge pull request #3959 from ns3777k/feature/hook-direct-name

add optional name arg to .hook function

Conflicts:
	docs/docs/hooks.md
	test/integration/hooks.test.js
2 parents 7b333af3 6b4efd6e
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
- [FIXED] $or/$and inside a where clause always expects the input to be an array [#3767](https://github.com/sequelize/sequelize/issues/3767) - [FIXED] $or/$and inside a where clause always expects the input to be an array [#3767](https://github.com/sequelize/sequelize/issues/3767)
- [ADDED] Unique constraints may now include custom error messages - [ADDED] Unique constraints may now include custom error messages
- [ADDED] It's possible now to remove a hook by name - [ADDED] It's possible now to remove a hook by name
- [ADDED] Hook name can be passed via the direct method [#3901](https://github.com/sequelize/sequelize/issues/3901)
# 3.2.0 # 3.2.0
- [FEATURE] Add support for new option `targetKey` in a belongs-to relationship for situations where the target key is not the id field. - [FEATURE] Add support for new option `targetKey` in a belongs-to relationship for situations where the target key is not the id field.
......
...@@ -73,7 +73,7 @@ User.beforeCreate(function(user, options) { ...@@ -73,7 +73,7 @@ User.beforeCreate(function(user, options) {
}); });
}) })
User.afterValidate(function(user, options) { User.afterValidate('myHookAfter', function(user, options, fn) {
user.username = 'Toni' user.username = 'Toni'
}) })
``` ```
......
...@@ -373,8 +373,8 @@ module.exports = { ...@@ -373,8 +373,8 @@ module.exports = {
var allHooks = Object.keys(hookTypes).concat(Object.keys(hookAliases)); var allHooks = Object.keys(hookTypes).concat(Object.keys(hookAliases));
allHooks.forEach(function(hook) { allHooks.forEach(function(hook) {
Model.prototype[hook] = function(callback) { Model.prototype[hook] = function(name, callback) {
return this.addHook(hook, callback); return this.addHook(hook, name, callback);
}; };
}); });
} }
......
...@@ -208,20 +208,26 @@ describe(Support.getTestDialectTeaser('Hooks'), function() { ...@@ -208,20 +208,26 @@ describe(Support.getTestDialectTeaser('Hooks'), function() {
describe('#removeHook', function() { describe('#removeHook', function() {
it('should remove hook', function() { it('should remove hook', function() {
var hook1 = sinon.spy(); var hook1 = sinon.spy()
, hook2 = sinon.spy();
this.Model.addHook('beforeCreate', 'myHook', hook1); this.Model.addHook('beforeCreate', 'myHook', hook1);
this.Model.beforeCreate('myHook2', hook2);
return this.Model.runHooks('beforeCreate').bind(this).then(function() { return this.Model.runHooks('beforeCreate').bind(this).then(function() {
expect(hook1).to.have.been.calledOnce; expect(hook1).to.have.been.calledOnce;
expect(hook2).to.have.been.calledOnce;
hook1.reset(); hook1.reset();
hook2.reset();
this.Model.removeHook('beforeCreate', 'myHook'); this.Model.removeHook('beforeCreate', 'myHook');
this.Model.removeHook('beforeCreate', 'myHook2');
return this.Model.runHooks('beforeCreate'); return this.Model.runHooks('beforeCreate');
}).then(function() { }).then(function() {
expect(hook1).not.to.have.been.called; expect(hook1).not.to.have.been.called;
expect(hook2).not.to.have.been.called;
}); });
}); });
}); });
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!