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

Commit 71b4a3e4 by Sascha Gehlich

Fixed hook aliasing. Aliases in hooks hash are now replaced with real hook types.

1 parent b4ec3624
...@@ -53,6 +53,7 @@ module.exports = (function() { ...@@ -53,6 +53,7 @@ module.exports = (function() {
} }
attributes = replaceReferencesWithTableNames(attributes) attributes = replaceReferencesWithTableNames(attributes)
this.options.hooks = this.replaceHookAliases(this.options.hooks)
this.rawAttributes = attributes this.rawAttributes = attributes
this.daoFactoryManager = null // defined in init function this.daoFactoryManager = null // defined in init function
......
var Utils = require("./utils")
var Hooks = module.exports = function(){} var Hooks = module.exports = function(){}
var hookAliases = {
beforeDelete: "beforeDestroy",
afterDelete: "afterDestroy"
}
Hooks.replaceHookAliases = function(hooks) {
var realHookName
Utils._.each(hooks, function(hooksArray, name) {
// Does an alias for this hook name exist?
if(realHookName = hookAliases[name]) {
// Add the hooks to the actual hook
hooks[realHookName] = (hooks[realHookName] || []).concat(hooksArray)
// Delete the alias
delete hooks[name]
}
})
return hooks
}
Hooks.runHooks = function() { Hooks.runHooks = function() {
var self = this var self = this
...@@ -43,16 +65,9 @@ Hooks.runHooks = function() { ...@@ -43,16 +65,9 @@ Hooks.runHooks = function() {
run(hooks[tick]) run(hooks[tick])
} }
Hooks.hook = function(hookType, name, fn) { // Alias for `.addHook`
// For aliases, we may want to incorporate some sort of way to mitigate this Hooks.hook = function() {
if (hookType === "beforeDelete") { Hooks.addHook.apply(this, arguments)
hookType = 'beforeDestroy'
}
else if (hookType === "afterDelete") {
hookType = 'afterDestroy'
}
Hooks.addHook.call(this, hookType, name, fn)
} }
Hooks.addHook = function(hookType, name, fn) { Hooks.addHook = function(hookType, name, fn) {
...@@ -65,6 +80,9 @@ Hooks.addHook = function(hookType, name, fn) { ...@@ -65,6 +80,9 @@ Hooks.addHook = function(hookType, name, fn) {
fn.apply(this, Array.prototype.slice.call(arguments, 0, arguments.length-1).concat(arguments[arguments.length-1])) fn.apply(this, Array.prototype.slice.call(arguments, 0, arguments.length-1).concat(arguments[arguments.length-1]))
} }
// Aliases
hookType = hookAliases[hookType] || hookType
// Just in case if we override the default DAOFactory.options // 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: method} : method this.options.hooks[hookType][this.options.hooks[hookType].length] = !!name ? {name: name, fn: method} : method
...@@ -95,11 +113,11 @@ Hooks.afterDestroy = function(name, fn) { ...@@ -95,11 +113,11 @@ Hooks.afterDestroy = function(name, fn) {
} }
Hooks.beforeDelete = function(name, fn) { Hooks.beforeDelete = function(name, fn) {
Hooks.addHook.call(this, 'beforeDestroy', name, fn) Hooks.addHook.call(this, 'beforeDelete', name, fn)
} }
Hooks.afterDelete = function(name, fn) { Hooks.afterDelete = function(name, fn) {
Hooks.addHook.call(this, 'afterDestroy', name, fn) Hooks.addHook.call(this, 'afterDelete', name, fn)
} }
Hooks.beforeUpdate = function(name, fn) { Hooks.beforeUpdate = function(name, fn) {
......
...@@ -6995,7 +6995,7 @@ describe(Support.getTestDialectTeaser("Hooks"), function () { ...@@ -6995,7 +6995,7 @@ describe(Support.getTestDialectTeaser("Hooks"), function () {
}) })
}) })
describe.only('passing DAO instances', function() { describe('passing DAO instances', function() {
describe('beforeValidate / afterValidate', function() { describe('beforeValidate / afterValidate', function() {
it('should pass a DAO instance to the hook', function(done){ it('should pass a DAO instance to the hook', function(done){
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!