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

Commit 00246ecf by Sascha Gehlich

before and after hooks now get the DAO instance instead of only the dataValues

1 parent 3b2c0ee0
......@@ -23,12 +23,11 @@ DaoValidator.prototype.hookValidate = function() {
, errors = {}
return new Utils.CustomEventEmitter(function(emitter) {
self.model.daoFactory.runHooks('beforeValidate', self.model.dataValues, function(err, newValues) {
self.model.daoFactory.runHooks('beforeValidate', self.model, function(err) {
if (!!err) {
return emitter.emit('error', err)
}
self.model.dataValues = newValues || self.model.dataValues
errors = Utils._.extend(errors, validateAttributes.call(self))
errors = Utils._.extend(errors, validateModel.call(self))
......@@ -36,12 +35,11 @@ DaoValidator.prototype.hookValidate = function() {
return emitter.emit('error', errors)
}
self.model.daoFactory.runHooks('afterValidate', self.model.dataValues, function(err, newValues) {
self.model.daoFactory.runHooks('afterValidate', self.model, function(err) {
if (!!err) {
return emitter.emit('error', err)
}
self.model.dataValues = newValues || self.model.dataValues
emitter.emit('success', self.model)
})
})
......
......@@ -375,35 +375,40 @@ module.exports = (function() {
hook = 'Update'
}
self.__factory.runHooks('before' + hook, values, function(err, newValues) {
// Add the values to the DAO
self.dataValues = _.extend(self.dataValues, values)
// Run the beforeCreate / beforeUpdate hook
self.__factory.runHooks('before' + hook, self, function(err) {
if (!!err) {
return emitter.emit('error', err)
}
args[2] = values = newValues || values
// Newest values need to be on the dao since the dao is returned from the query interface
self.dataValues = _.extend(self.dataValues, values)
// dataValues might have changed inside the hook, rebuild
// the values hash
values = {}
options.fields.forEach(function(field) {
if (self.dataValues[field] !== undefined) {
values[field] = self.dataValues[field]
}
})
args[2] = values
self.QueryInterface[query].apply(self.QueryInterface, args)
.proxy(emitter, {events: ['sql', 'error']})
.success(function(result) {
// Transfer database generated values (defaults, autoincrement, etc)
values = _.extend(values, result.values)
values = _.extend(values, result.dataValues)
// Ensure new values are on DAO, and reset previousDataValues
result.dataValues = _.extend(result.dataValues, values)
result._previousDataValues = _.clone(result.dataValues)
self.__factory.runHooks('after' + hook, values, function(err, newValues) {
self.__factory.runHooks('after' + hook, result, function(err) {
if (!!err) {
return emitter.emit('error', err)
}
if (newValues) {
// Repeat value assignment incase afterHook changed anything
result.dataValues = _.extend(result.dataValues, newValues)
result._previousDataValues = _.clone(result.dataValues)
}
emitter.emit('success', result)
})
})
......@@ -486,7 +491,7 @@ module.exports = (function() {
, query = null
return new Utils.CustomEventEmitter(function(emitter) {
self.daoFactory.runHooks(self.daoFactory.options.hooks.beforeDestroy, self.dataValues, function(err) {
self.daoFactory.runHooks(self.daoFactory.options.hooks.beforeDestroy, self, function(err) {
if (!!err) {
return emitter.emit('error', err)
}
......@@ -507,7 +512,7 @@ module.exports = (function() {
emitter.emit('error', err)
})
.success(function(results) {
self.daoFactory.runHooks(self.daoFactory.options.hooks.afterDestroy, self.dataValues, function(err) {
self.daoFactory.runHooks(self.daoFactory.options.hooks.afterDestroy, self, function(err) {
if (!!err) {
return emitter.emit('error', err)
}
......
......@@ -6994,4 +6994,138 @@ describe(Support.getTestDialectTeaser("Hooks"), function () {
})
})
})
describe('passing DAO instances', function() {
describe('beforeValidate / afterValidate', function() {
it('should pass a DAO instance to the hook', function(done){
var User = this.sequelize.define('User', {
username: DataTypes.STRING
}, {
hooks: {
beforeValidate: function(user, fn) {
expect(user).to.be.instanceof(User.DAO)
fn()
},
afterValidate: function(user, fn) {
expect(user).to.be.instanceof(User.DAO)
fn()
}
}
})
User.sync({ force: true }).success(function() {
User.create({ username: 'bob' }).success(function(user) {
done()
})
})
})
})
describe('beforeCreate / afterCreate', function() {
it('should pass a DAO instance to the hook', function(done){
var User = this.sequelize.define('User', {
username: DataTypes.STRING
}, {
hooks: {
beforeCreate: function(user, fn) {
expect(user).to.be.instanceof(User.DAO)
fn()
},
afterCreate: function(user, fn) {
expect(user).to.be.instanceof(User.DAO)
fn()
}
}
})
User.sync({ force: true }).success(function() {
User.create({ username: 'bob' }).success(function(user) {
done()
})
})
})
})
describe('beforeDestroy / afterDestroy', function() {
it('should pass a DAO instance to the hook', function(done){
var User = this.sequelize.define('User', {
username: DataTypes.STRING
}, {
hooks: {
beforeDestroy: function(user, fn) {
expect(user).to.be.instanceof(User.DAO)
fn()
},
afterDestroy: function(user, fn) {
expect(user).to.be.instanceof(User.DAO)
fn()
}
}
})
User.sync({ force: true }).success(function() {
User.create({ username: 'bob' }).success(function(user) {
user.destroy().success(function() {
done()
})
})
})
})
})
describe('beforeDelete / afterDelete', function() {
it('should pass a DAO instance to the hook', function(done){
var User = this.sequelize.define('User', {
username: DataTypes.STRING
}, {
hooks: {
beforeDelete: function(user, fn) {
expect(user).to.be.instanceof(User.DAO)
fn()
},
afterDelete: function(user, fn) {
expect(user).to.be.instanceof(User.DAO)
fn()
}
}
})
User.sync({ force: true }).success(function() {
User.create({ username: 'bob' }).success(function(user) {
user.destroy().success(function() {
done()
})
})
})
})
})
describe('beforeUpdate / afterUpdate', function() {
it('should pass a DAO instance to the hook', function(done){
var User = this.sequelize.define('User', {
username: DataTypes.STRING
}, {
hooks: {
beforeUpdate: function(user, fn) {
expect(user).to.be.instanceof(User.DAO)
fn()
},
afterUpdate: function(user, fn) {
expect(user).to.be.instanceof(User.DAO)
fn()
}
}
})
User.sync({ force: true }).success(function() {
User.create({ username: 'bob' }).success(function(user) {
user.save({ username: 'bawb' }).success(function() {
done()
})
})
})
})
})
})
})
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!