Skip to content

Commit

Permalink
Merge pull request #23 from yoshuawuyts/multi-match
Browse files Browse the repository at this point in the history
on(): enable multiple handlers
  • Loading branch information
yoshuawuyts committed Jun 15, 2015
2 parents e0b0778 + 2ff5a66 commit 0985161
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ Initialize a router with a default route. Doesn't ignore querystrings and hashes

### router.on(route, cb(params))
Register a new route. The order in which routes are registered does not matter.
See [`routington.define()`](https://github.com/pillarjs/routington#nodes-node--routerdefineroute)
Multiple callbacks can be registered. See
[`routington.define()`](https://github.com/pillarjs/routington#nodes-node--routerdefineroute)
for all route options.

### router(route)
Expand Down
7 changes: 5 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ function wayfarer (dft) {
assert.equal(typeof cb, 'function')
path = sanitizeUri(path) || ''
const node = cb[sym] ? mounts.define(path)[0] : router.define(path)[0]
node.cb = cb
if (Array.isArray(node.cb)) node.cb.push(cb)
else node.cb = [cb]
return emit
}

Expand All @@ -45,7 +46,9 @@ function wayfarer (dft) {
params = xtend(params, match.param)

// only nested routers need a path
sub ? match.node.cb(path, params) : match.node.cb(params)
match.node.cb.forEach(function (cb) {
sub ? cb(path, params) : cb(params)
})
}

// match a mounted router
Expand Down
14 changes: 14 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ test('.emit() should throw if no matches are found', function (t) {
t.throws(r1.bind(r1, '/woops'), /path/)
})

test('.emi() should allow multiple handlers', function (t) {
t.plan(2)

const r1 = wayfarer()
r1.on('/', function () {
t.pass('call 1')
})
r1.on('/', function () {
t.pass('call 2')
})

r1('/')
})

test('.emit() should allow nesting', function (t) {
t.plan(7)

Expand Down

0 comments on commit 0985161

Please sign in to comment.