Skip to content

Commit

Permalink
Adding pausing #18
Browse files Browse the repository at this point in the history
  • Loading branch information
Krasimir Tsonev committed Jun 18, 2016
1 parent 5d72b17 commit cbc8559
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 4 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,18 @@ The resolving of the routes happen when `resolve` method is fired which happen:
* every time when the page's URL changes
* if you call `navigate`

### Pausing the router

[Sometimes](https://github.com/krasimir/navigo/issues/18) you need to update the URL but you don't want to resolve your callbacks. In such cases you may call `.pause(true)` and do `.navigate('new/url/here')`. For example:

```js
r.pause(true);
r.navigate('/en/products');
r.pause(false);
```

The route will be changed to `/en/products` but if you have a handler for that path will not be executed.

## API

* `router.on(function)` - adding a new route
Expand All @@ -175,6 +187,7 @@ The resolving of the routes happen when `resolve` method is fired which happen:
* `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.
* `router.link(path)` - it returns a full url of the given `path`
* `router.pause(boolean)` - it gives you a chance to change the route without resolving. Make sure that you call `router.pause(false)` so you return to the previous working state.

## Tests

Expand Down
29 changes: 29 additions & 0 deletions demo/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,33 @@ describe('Given Navigo library', function () {
r.navigate('test-case/user/42');
});
});
describe('when we update the internal state', function () {
beforeEach(function () {
r = new Navigo(root);
});
afterEach(function () {
r.destroy();
r.navigate('testing');
});
it('should not fire the resolve method', function (done) {
this.timeout(3000);
var lang = '';

r.on('/:lang/products', function (params) {
lang = params.lang;
});
r.navigate('/en/products');
r.pause(true);
r.navigate('/foo/products');
setTimeout(function () {
r.navigate('/bar/products');
}, 500);
setTimeout(function () {
r.pause(false);
r.navigate('/bg/products');
expect(lang).to.be.equal('bg');
done();
}, 1000);
});
});
});
5 changes: 5 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 lib/navigo.min.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.min.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": "2.0.0",
"version": "2.0.1",
"description": "A simple vanilla JavaScript router with a fallback for older browsers",
"main": "lib/navigo.js",
"jsnext:main": "src/index.js",
Expand Down
5 changes: 5 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ function Navigo(r, useHash) {
this._routes = [];
this.root = useHash && r ? r.replace(/\/$/, '/#') : (r || null);
this._useHash = useHash;
this._paused = false;
this._ok = !useHash && !!(
typeof window !== 'undefined' &&
window.history &&
Expand Down Expand Up @@ -121,6 +122,7 @@ Navigo.prototype = {
var handler, m;
var url = (current || this._cLoc()).replace(this._getRoot(), '');

if (this._paused) return false;
if (this._useHash) {
url = url.replace(/^\/#/, '/');
}
Expand Down Expand Up @@ -173,6 +175,9 @@ Navigo.prototype = {
link: function (path) {
return this._getRoot() + path;
},
pause: function (status) {
this._paused = status;
},
_add: function (route, handler = null) {
if (typeof handler === 'object') {
this._routes.push({ route, handler: handler.uses, name: handler.as });
Expand Down

0 comments on commit cbc8559

Please sign in to comment.