0.5.23 • Published 8 years ago

x-router v0.5.23

Weekly downloads
79
License
MIT
Repository
github
Last release
8 years ago

x-router

NPM Version NPM Downloads NPM Downloads NPM Downloads Join the chat at https://gitter.im/attrs/x-router

frontend router

Install

$ npm install x-router --save
var xrouter = require('x-router');
var app = xrouter().use(...).listen();

Usage

var app = xrouter()
  .config('view target', '#target1')  // default render target
  .config('views', '/')
  .use(function(req, res, next) {
    console.log('hello');
    next();
  })
  .get('/', function(req, res, next) {
    res.render.html('Hello!');
  })
  .use('/sub', xrouter.Router()
     .use(function(req, res, next) {
       console.log('sub routing...');
       res.set('view target', '#target2'); // change render target dynamically
       next();
     })
     .get('/', 'index')  // redirect to `index`
     .get('/index', function(req, res, next) {
       res.render.html('sub index!',  {
         target: '#target3'
       }).end();
     })
     .get('/some', function(req, res, next) {
       res.end();
     })
     .get('/:value', function(req, res, next) {
       var value = req.params.value;
       
       res.render.html('parameter is ' + value, function(err, target) {
         if( err ) return next(err);
         console.log('render target is ', target);
       }).end();
     })
  )
  .on('end', function(e) {
    console.debug('end', e.detail.href);
  })
  .on('writestate', function(e) {
    console.debug('writestate', e.detail);
  })
  .on('notfound', function(e) {
    console.warn('notfound', e.detail.href);
  })
  .on('error', function(e) {
    console.error('error', e.detail.error);
  })
  .listen();
<!DOCTYPE html>

<html>
<head>
  <title></title>
  <meta charset="utf-8">
  <meta name="xrouter.mode" content="auto">
  <script src="dist/x-router.js"></script>
  <script src="app.js"></script>
</head>
<body>
  <a href="/" route>home</a>
  <a href="/sub" route>/sub</a>
  <a href="/sub/index" route>/sub/index</a>
  <a href="/sub/some" route>/sub/some</a>
  <a href="/sub/other" route>/sub/other</a>
  
  <h3>target1</h3>
  <div id="target1"></div>
  
  <h3>target2</h3>
  <div id="target2"></div>
  
  <h3>target3</h3>
  <div id="target3"></div>
</body>
</html>

Configuration

support both pushstate and hash, If you have not set up any value automatically using pushstate or hashbang(#!/path).

<meta name="xrouter.mode" content="pushstate | hashbang | hash | auto">
<meta name="xrouter.debug" content="false | true">
<meta name="xrouter.observe" content="true | false">
<meta name="xrouter.observe.delay" content="1000">

HTML

<a href="/a/b/c/d/e" route>/a/b</a>
<a href="/a/b/c/d/e" route ghost>/a/c</a>
<a href="javascript:xrouter.href('/a/b/c/d');">xrouter.href('/a/b/c/d')</a>

Related Project

License

Licensed under the MIT License. See LICENSE for the full license text.

0.5.23

8 years ago

0.5.22

8 years ago

0.5.21

8 years ago

0.5.20

8 years ago

0.5.19

8 years ago

0.5.18

9 years ago

0.5.17

9 years ago

0.5.16

9 years ago

0.5.15

9 years ago

0.5.14

9 years ago

0.5.13

9 years ago

0.5.12

9 years ago

0.5.11

9 years ago

0.5.10

9 years ago

0.5.9

9 years ago

0.5.8

9 years ago

0.5.7

9 years ago

0.5.6

9 years ago

0.5.5

9 years ago

0.5.4

9 years ago

0.5.3

9 years ago

0.4.5

9 years ago

0.5.2

9 years ago

0.5.1

9 years ago

0.5.0

9 years ago

0.4.4

9 years ago

0.4.3

9 years ago

0.4.2

9 years ago

0.4.1

9 years ago

0.4.0

9 years ago

0.3.30

9 years ago

0.3.29

9 years ago

0.3.28

9 years ago

0.3.27

9 years ago

0.3.26

9 years ago

0.3.25

10 years ago

0.3.24

10 years ago

0.3.23

10 years ago

0.3.22

10 years ago

0.3.21

10 years ago

0.3.20

10 years ago

0.3.19

10 years ago

0.3.18

10 years ago

0.3.17

10 years ago

0.3.16

10 years ago

0.3.15

10 years ago

0.3.14

10 years ago

0.3.13

10 years ago

0.3.12

10 years ago

0.3.11

10 years ago

0.3.10

10 years ago

0.3.9

10 years ago

0.3.8

10 years ago

0.3.7

10 years ago

0.3.6

10 years ago

0.3.5

10 years ago

0.3.4

10 years ago

0.3.3

10 years ago

0.3.2

10 years ago

0.3.1

10 years ago

0.3.0

10 years ago

0.2.6

10 years ago

0.2.5

10 years ago

0.2.4

10 years ago

0.2.3

10 years ago

0.2.2

10 years ago

0.2.1

10 years ago

0.2.0

10 years ago

0.1.2

10 years ago

0.1.1

10 years ago

0.1.0

10 years ago