url-dispatcher
v0.1.5
Published
A URL dispatcher like Backbone.Router.
Downloads
15
Readme
url-dispatcher
A URL dispatcher. It was inspired by Backbone.Router.
Installation
npm
npm install url-dispatcher --save
// import the module
var Dispatcher = require('url-dispatcher');
Bower
bower install url-dispatcher --save
<script src="/path/to/script/dispatcher.min.js"></script>
<!-- Or, if you want use on IE8 -->
<script src="/path/to/script/dispatcher.compat.min.js"></script>
Manually
git clone [email protected]:haribote/url-dispatcher.git
Or, Download ZIP
And then, copy dest/dispatcher.min.js
to your project.
<script src="/path/to/script/dispatcher.min.js"></script>
<!-- Or, if you want use on IE8 -->
<script src="/path/to/script/dispatcher.compat.min.js"></script>
Usage
// create instance with the routing map like Backbone.Router
var dispatcher = new Dispatcher({
routes: {
'/': function () {
return 'root';
},
'/hoge': function () {
return '/hoge';
},
'/hoge/:piyo': function (piyo) {
return {
piyo: piyo
};
},
'/hoge/:piyo/:fuga': function (piyo, fuga) {
return {
piyo: piyo,
fuga: fuga
};
},
'/hoge?piyo=:fuga&foo=:bar': (search, fuga, bar) => {
return {
search: search,
fuga : fuga,
bar : bar
};
},
'/foobar/*splat': function (path, search) {
return {
path : path,
search: search
};
}
}
});
// Call run method with URL String
dispatcher.run(location.href);
/*
* ex.) location.href = 'http://www.foobar.com/hoge/1/2'
* => {
* piyo: 1,
* fuga: 2
* }
*/