m-router
v1.0.1
Published
a tiny SPA router
Downloads
3
Readme
m-router
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