Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
public
/
sequelize
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
不要怂,就是干,撸起袖子干!
Commit 71b4a3e4
authored
Jan 09, 2014
by
Sascha Gehlich
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixed hook aliasing. Aliases in hooks hash are now replaced with real hook types.
1 parent
b4ec3624
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
32 additions
and
13 deletions
lib/dao-factory.js
lib/hooks.js
test/hooks.test.js
lib/dao-factory.js
View file @
71b4a3e
...
...
@@ -53,6 +53,7 @@ module.exports = (function() {
}
attributes
=
replaceReferencesWithTableNames
(
attributes
)
this
.
options
.
hooks
=
this
.
replaceHookAliases
(
this
.
options
.
hooks
)
this
.
rawAttributes
=
attributes
this
.
daoFactoryManager
=
null
// defined in init function
...
...
lib/hooks.js
View file @
71b4a3e
var
Utils
=
require
(
"./utils"
)
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
()
{
var
self
=
this
...
...
@@ -43,16 +65,9 @@ Hooks.runHooks = function() {
run
(
hooks
[
tick
])
}
Hooks
.
hook
=
function
(
hookType
,
name
,
fn
)
{
// For aliases, we may want to incorporate some sort of way to mitigate this
if
(
hookType
===
"beforeDelete"
)
{
hookType
=
'beforeDestroy'
}
else
if
(
hookType
===
"afterDelete"
)
{
hookType
=
'afterDestroy'
}
Hooks
.
addHook
.
call
(
this
,
hookType
,
name
,
fn
)
// Alias for `.addHook`
Hooks
.
hook
=
function
()
{
Hooks
.
addHook
.
apply
(
this
,
arguments
)
}
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
]))
}
// Aliases
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
].
length
]
=
!!
name
?
{
name
:
name
,
fn
:
method
}
:
method
...
...
@@ -95,11 +113,11 @@ Hooks.afterDestroy = function(name, fn) {
}
Hooks
.
beforeDelete
=
function
(
name
,
fn
)
{
Hooks
.
addHook
.
call
(
this
,
'beforeDe
stroy
'
,
name
,
fn
)
Hooks
.
addHook
.
call
(
this
,
'beforeDe
lete
'
,
name
,
fn
)
}
Hooks
.
afterDelete
=
function
(
name
,
fn
)
{
Hooks
.
addHook
.
call
(
this
,
'afterDe
stroy
'
,
name
,
fn
)
Hooks
.
addHook
.
call
(
this
,
'afterDe
lete
'
,
name
,
fn
)
}
Hooks
.
beforeUpdate
=
function
(
name
,
fn
)
{
...
...
test/hooks.test.js
View file @
71b4a3e
...
...
@@ -6995,7 +6995,7 @@ describe(Support.getTestDialectTeaser("Hooks"), function () {
})
})
describe
.
only
(
'passing DAO instances'
,
function
()
{
describe
(
'passing DAO instances'
,
function
()
{
describe
(
'beforeValidate / afterValidate'
,
function
()
{
it
(
'should pass a DAO instance to the hook'
,
function
(
done
){
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment