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

Commit 0e4c7a60 by Mick Hansen

fix(hooks): normalize Model.hooks to arrays (if a user passed a single method it…

… failed), closes #3179
1 parent 03612872
...@@ -152,12 +152,10 @@ Hooks.addHook = function(hookType, name, fn) { ...@@ -152,12 +152,10 @@ Hooks.addHook = function(hookType, name, fn) {
name = null; name = null;
} }
// Aliases
hookType = hookAliases[hookType] || hookType; hookType = hookAliases[hookType] || hookType;
// Just in case if we override the default DAOFactory.options
this.options.hooks[hookType] = this.options.hooks[hookType] || []; this.options.hooks[hookType] = this.options.hooks[hookType] || [];
this.options.hooks[hookType][this.options.hooks[hookType].length] = !!name ? {name: name, fn: fn} : fn; this.options.hooks[hookType].push(!!name ? {name: name, fn: fn} : fn);
return this; return this;
}; };
......
...@@ -46,7 +46,11 @@ module.exports = (function() { ...@@ -46,7 +46,11 @@ module.exports = (function() {
this.associations = {}; this.associations = {};
this.modelManager = this.daoFactoryManager = null; this.modelManager = this.daoFactoryManager = null;
this.name = name; this.name = name;
this.options.hooks = this.replaceHookAliases(this.options.hooks); this.options.hooks = _.mapValues(this.replaceHookAliases(this.options.hooks), function (hooks) {
if (!Array.isArray(hooks)) hooks = [hooks];
return hooks;
});
this.scopeObj = {}; this.scopeObj = {};
this.sequelize = options.sequelize; this.sequelize = options.sequelize;
this.underscored = this.underscored || this.underscoredAll; this.underscored = this.underscored || this.underscoredAll;
......
...@@ -5877,4 +5877,27 @@ describe(Support.getTestDialectTeaser('Hooks'), function() { ...@@ -5877,4 +5877,27 @@ describe(Support.getTestDialectTeaser('Hooks'), function() {
}); });
}); });
}); });
describe('#addHook', function() {
it('should add additinoal hook when previous exists', function() {
var hook1 = sinon.spy()
, hook2 = sinon.spy()
, Model;
Model = this.sequelize.define('Model', {
name: Sequelize.STRING
}, {
hooks: { beforeCreate: hook1 }
});
Model.addHook('beforeCreate', hook2);
return Model.sync({ force: true }).then(function() {
return Model.create({ name: 'bob' });
}).then(function() {
expect(hook1.calledOnce).to.be.ok;
expect(hook2.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!