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

Commit 00831299 by ns3777k Committed by Jan Aagaard Meier

Added functionality to remove hooks. Merge pull request #3947 from ns3777k:feature/remove-hooks

1 parent 323b8cc5
......@@ -3,6 +3,7 @@
- [FIXED] Creating and dropping enums in transaction, only for PostgreSQL [#3782](https://github.com/sequelize/sequelize/issues/3782)
- [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] It's possible now to remove a hook by name
# 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.
......
......@@ -162,6 +162,31 @@ var Hooks = {
return this;
},
/**
* Remove hook from the model
*
* @param {String} hookType
* @param {String} name
*/
removeHook: function(hookType, name) {
hookType = hookAliases[hookType] || hookType;
if (!this.hasHook(hookType)) {
return this;
}
this.options.hooks[hookType] = this.options.hooks[hookType].filter(function (hook) {
// don't remove unnamed hooks
if (typeof hook === 'function') {
return true;
}
return typeof hook === 'object' && hook.name !== name;
});
return this;
},
/*
* Check whether the mode has any hooks of this type
*
......
......@@ -5623,7 +5623,7 @@ describe(Support.getTestDialectTeaser('Hooks'), function() {
});
describe('#addHook', function() {
it('should add additinoal hook when previous exists', function() {
it('should add additional hook when previous exists', function() {
var hook1 = sinon.spy()
, hook2 = sinon.spy()
, Model;
......@@ -5644,4 +5644,27 @@ describe(Support.getTestDialectTeaser('Hooks'), function() {
});
});
});
describe('#removeHook', function() {
it('should remove hook', function() {
var hook1 = sinon.spy()
, Model;
Model = this.sequelize.define('Model', {
name: Sequelize.STRING
});
Model.addHook('beforeCreate', 'myHook', hook1);
return Model.sync({ force: true }).then(function() {
return Model.create({ name: 'sonya' });
}).then(function() {
expect(hook1.calledOnce).to.be.ok;
Model.removeHook('beforeCreate', 'myHook');
return Model.create({ name: 'james' });
}).then(function() {
expect(hook1.calledOnce).to.be.ok;
});
});
});
});
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!