m-router

1.0.1 • Public • Published

m-router

npm GitHub issues GitHub license

example

// router.js
import Router  from 'm-router'
import Xtemplate from 'xtemplate/lib/runtime'
import controller from 'controller'

let router = new Router({
  render(xtpl, data, callback) {
    document.getElementById('root').innerHTML = new Xtemplate(xtpl).render(data)
    this.runPage(callback)
    $('body').scrollTop(0) // to top
  }
})


router.get('/url/a', controller.a)
router.get('/url/b', controller.b)


router.start()


// controller.js

// webpack Dynamic Imports https://webpack.js.org/guides/code-splitting/#dynamic-imports

export default {
    a: function(req, res) {
      import(/* webpackChunkName: "a" */ 'script/a').then(module => {
        if (this.needBrowserRender(module.default)) {
          res.render(
            require('views/a.xtpl'),
            {
              title: 'a'
            },
            module.default)
        }
      })
    },
      b: function(req, res) {
        import(/* webpackChunkName: "b" */ 'script/b').then(module => {
          if (this.needBrowserRender(module.default)) {
            res.render(
              require('views/b.xtpl'),
              {
                title: 'b'
              },
              module.default)
          }
        })
      },
}

// a.js

export default function() {
  console.log('page load')
  return function() {
    console.log('page left')
  }
}

```javascript

Readme

Keywords

Package Sidebar

Install

npm i m-router

Weekly Downloads

1

Version

1.0.1

License

MIT

Last publish

Collaborators

  • hpoenixf