Skip to content

Commit

Permalink
Adding off method #72
Browse files Browse the repository at this point in the history
  • Loading branch information
Krasimir Tsonev committed Mar 9, 2017
1 parent d03d370 commit 498ee2c
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 4.1.1

* Adding `off` API method [#72](https://github.com/krasimir/navigo/issues/72).

## 4.0.1

* Fixing [#77](https://github.com/krasimir/navigo/issues/77/).
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ You may provide hooks in two other cases:
* `router.on(function)` - adding handler for root/main route
* `router.on(string, function)` - adding a new route
* `router.on(object)` - adding a new route
* `router.off(handler)` - removes the routes associated with the given handler/function
* `router.navigate(path='', absolute=false)` - if `absolute` is `false` then Navigo finds the root path of your app based on the provided routes.
* `router.resolve(currentURL=undefined)` - if `currentURL` is provided then the method tries resolving the registered routes to that URL and not `window.location.href`.
* `router.destroy` - removes all the registered routes and stops the URL change listening.
Expand Down
12 changes: 12 additions & 0 deletions lib/navigo.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/navigo.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "navigo",
"version": "4.0.1",
"version": "4.1.1",
"description": "A simple vanilla JavaScript router with a fallback for older browsers",
"main": "lib/navigo.js",
"jsnext:main": "src/index.js",
Expand Down
12 changes: 12 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,18 @@ Navigo.prototype = {
}
return this;
},
off: function (handler) {
if (this._defaultHandler !== null && handler === this._defaultHandler.handler) {
this._defaultHandler = null;
} else if (this._notFoundHandler !== null && handler === this._notFoundHandler.handler) {
this._notFoundHandler = null;
}
this._routes = this._routes.reduce((result, r) => {
if (r.handler !== handler) result.push(r);
return result;
}, []);
return this;
},
notFound: function (handler, hooks) {
this._notFoundHandler = { handler, hooks: hooks };
return this;
Expand Down
18 changes: 14 additions & 4 deletions test/spec/Navigo.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ describe('Given an instance of Navigo', function () {
});
});

describe('and we use the off method', function () {
it('should remove the route handler', function () {
handler = sinon.spy();
router.on(handler).off(handler).resolve();
router.on('/test', handler).off(handler).resolve();
router.notFound(handler).off(handler).resolve();
expect(handler).to.not.be.calledOnce;
});
});

describe('and when we pass handler with no matching pattern', function () {
it('should call the handler', function () {
handler = sinon.spy();
Expand Down Expand Up @@ -341,9 +351,9 @@ describe('Given an instance of Navigo', function () {
beforeHook.callArg(0);
expect(handler).to.be.calledOnce;
});
it('should not call the handler if the before hook returns false', function() {
var beforeHook = sinon.spy(function(cb) {
cb(false)
it('should not call the handler if the before hook returns false', function () {
var beforeHook = sinon.spy(function (cb) {
cb(false);
});
var handler = sinon.spy();

Expand All @@ -353,7 +363,7 @@ describe('Given an instance of Navigo', function () {

expect(beforeHook).to.be.calledOnce;
expect(handler).to.not.be.called;
})
});
});
describe('and we provide only after hook', function () {
it('should call after hook + the handler', function () {
Expand Down

0 comments on commit 498ee2c

Please sign in to comment.