Skip to content

Commit

Permalink
feat(addHook): accept an array of hooks too
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Dec 7, 2017
1 parent b5a9ef9 commit 6abaa6a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/Lucid/Model/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,13 +305,13 @@ class Model extends BaseModel {
* @method addHook
*
* @param {String} forEvent
* @param {Function|String} handler
* @param {Function|String|Array} handlers
*
* @chainable
*
* @static
*/
static addHook (forEvent, handler) {
static addHook (forEvent, handlers) {
const [cycle, event] = util.getCycleAndEvent(forEvent)

/**
Expand All @@ -324,7 +324,10 @@ class Model extends BaseModel {
/**
* Add the handler
*/
this.$hooks[cycle].addHandler(event, handler)
handlers = Array.isArray(handlers) ? handlers : [handlers]
handlers.forEach((handler) => {
this.$hooks[cycle].addHandler(event, handler)
})
return this
}

Expand Down
9 changes: 9 additions & 0 deletions test/unit/lucid.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,15 @@ test.group('Model', (group) => {
assert.lengthOf(User.$hooks.after._handlers.create, 1)
})

test('add hooks as an array', async (assert) => {
class User extends Model {
}

User._bootIfNotBooted()
User.addHook('beforeCreate', [function () {}, function () {}])
assert.lengthOf(User.$hooks.before._handlers.create, 2)
})

test('throw exception when hook cycle is invalid', async (assert) => {
class User extends Model {
}
Expand Down

0 comments on commit 6abaa6a

Please sign in to comment.