$ npm install --save esr
import Esr from 'esr';
// create an instance.
const router = new Esr();
// routing definition.
const onEnter = function() {
// called when the url got changed to `/users`.
};
router.on('/users', onEnter);
// start listening for the url to change.
router.start();
// change the url.
router.navigateTo('/users');
Esr provides 3 different methods for creating a history object, depending on your environment.
import Esr from 'esr';
// use HTML5 history API.
const router = new Esr(Esr.BROWSER);
// use memory.
const router = new Esr(Esr.MEMORY);
// use hash.
const router = new Esr(Esr.HASH);
default is Esr.BROWSER
.
change current url. set secound parameter true
to force navigate.
import Esr from 'esr';
const router = new Esr();
router.navigateTo('/users');
router.navigateTo('/users/foo');
router.navigateTo('/users/foo?aaa=bbb');
router.navigateTo('/users/foo?aaa=bbb#ccc');
router.navigateTo('/users', true);
replace current url.
import Esr from 'esr';
const router = new Esr();
router.replace('/users');
start listening for the url to change. a boolean argument can be passed to determine whether the esr should fire route an event or not. default is true
.
import Esr from 'esr';
const router = new Esr();
router.on('/users', function(route) {
console.log('users');
});
// suppose that the current url is `https://sample.com/users`.
router.start();
//=> 'users'
router.on(pattern, onEnter, onBefore, onAfter)
for routing definition.
type: String
example: /users/:userid
set pattern to match.
Express-Style pattern is applied.
import Esr from 'esr';
const router = new Esr();
router.on('/users', function() {
console.log('users');
});
router.on('/users/:userid', function() {
console.log('a user');
});
router.on('*', function() {
console.log('not found');
});
router.navigateTo('/users');
//=> 'users'
router.navigateTo('/users/foo');
//=> 'a user'
router.navigateTo('/bar');
//=> 'not found'
type: Function
example: function(route)
a callback function that will be called when the url matches the pattern
.
import Esr from 'esr';
const router = new Esr();
router.on('/users/:userid', function(route) {
console.log('a user');
});
router.navigateTo('/users/foo');
//=> 'a user';
route
object consists of some extra information.
route.params
(Object) key-value data matched on thepattern
.route.queries
(Object) key-value data of query.route.hash
(String) hash value.
import Esr from 'esr';
const router = new Esr();
router.on('/users/:userid/:type', function(route) {
// route.params.userid === 'foo';
// route.params.type === 'bar';
// route.queries.aaa === 'AAA';
// route.queries.bbb === 'BBB';
// route.hash === 'ccc';
});
router.navigateTo('/users/foo/bar?aaa=AAA&bbb=BBB#ccc');
type: Function
example: function(route, replace)
a callback function that will be called just before onEnter
when the url matches the pattern
.
same as route
of onEnter
.
a function used to redirect to another url.
import Esr from 'esr';
const router = new Esr();
router.on('/aaa', function(route) {
console.log('onEnter of /aaa');
}, function(route, replace) {
console.log('onBefore of /aaa');
});
router.on('/bbb', function(route) {
console.log('onEnter of /bbb');
});
router.navigateTo('/aaa');
//=> 'onBefore of /aaa'
//(=> 'onEnter of /bbb') <= this won't be logged.
//=> 'onEnter of /bbb'
type: Function
example: function(route)
a callback function that will be called just after onEnter
when the url matches the pattern
.
same as route
of onEnter
.
callback functions that will be called for every url pattern.
router.onBeforeOnce
called only once. just beforerouter.on
's callback.router.onBefore
called just beforerouter.on
's callback.router.onAfter
called just afterrouter.on
's callback.router.onAfterOnce
called only once. just afterrouter.on
's callback.
import Esr from 'esr';
const router = new Esr();
router
.onBeforeOnce(function(route) {
console.log('before once');
})
.onBefore(function(route) {
console.log('before');
})
.on('*', function(route) => {
console.log('*');
})
.onAfter(function(route) {
console.log('after');
})
.onAfterOnce(function(route) {
console.log('after once');
});
router.navigateTo('/first');
//=> 'before once'
//=> 'before'
//=> '*'
//=> 'after'
//=> 'after once'
router.navigateTo('/second');
//=> 'before'
//=> '*'
//=> 'after'
By passing a callback function that returns a Promise, you can handle async programing.
import Esr from 'esr';
const router = new Esr();
router.on('/users',function(route) {
console.log('onEnter');
return new Promise(function(resolve) {
window.setTimeout(function() {
resolve();
}, 1000);
});
}, function(route, replace) {
console.log('onBefore');
return new Promise(function(resolve) {
window.setTimeout(function() {
resolve();
}, 1000);
});
}, function(route) {
console.log('onAfter');
return new Promise(function(resolve) {
window.setTimeout(function() {
resolve();
}, 1000);
});
}).onAfter(function(route) {
console.log('complete!');
});
router.navigateto('/users');
//=> 'onBefore'
// wait for 1000ms...
//=> 'onEnter'
// wait for 1000ms...
//=> 'onAfter'
// wait for 1000ms...
//=> 'complete!'
$ npm run test
$ npm run build
$ npm run watch
$ npm run lint