路由模块
- Source:
Methods
(static) checkMatchResult(hash, callback)
- Source:
检查当前路由是否能在路由映射表里找到,如果找不到匹配值就会触发传递给setup里的fail函数
Parameters:
Name | Type | Description |
---|---|---|
hash |
String | |
callback |
function |
(static) define(name, nexusopt, authorizeopt, callback)
- Source:
定义路由回调
Example
var router = new Router();
//一般定义
router.define('Page.Home', function() {
//todo something
})
//需要登陆验证
router.define('Page.Home', true, function() {
//todo something
});
//需要路由依赖
router.define('Page.Home', ['ui.Nav', 'ui.Header', 'ui.Footer'], function() {
//todo something
});
//需要路由依赖、登陆验证
router.define('Page.Home', ['ui.Nav', 'ui.Header', 'ui.Footer'], true, function() {
//todo something
});
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
name |
String | ||
nexus |
Object |
<optional> |
此路由所依赖的关系链 |
authorize |
Boolean |
<optional> |
是否需要登录操作 |
callback |
function | 回调函数,如果有路由关系链,将会等待关系链的成员准备好后再执行回调 |
(static) excludeAbandonModules()
- Source:
对当前路由依赖关系以及下一个路由依赖关系做对比,对不存在于路由关系链里的模块会销毁掉xjs.destroyView
(static) getAuthorization(hash)
- Source:
跳转到登陆模块,完成用户身份验证后再进入hash所匹配的路由
Parameters:
Name | Type | Description |
---|---|---|
hash |
(static) navigator(hashopt, stateopt, replaceHashopt)
- Source:
导航到下一个路由地址
Example
var router = new Router();
//默认情况,引导到#home并在历史记录里插入新的记录
router.navigator('#home');
//追加路由缓存信息
router.navigator('#home', {isHomePage: true});
console.log(history.state.isHomePage) // true
//提换当前路由历史记录
router.navigator('#home', {}, true);
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
hash |
String |
<optional> |
下一个路由的Hash地址,不填写则默认引导到#home |
state |
Object |
<optional> |
路由的缓存内容,将会存储到history.state对象里 |
replaceHash |
Boolean |
<optional> |
当设置为true时会提换历史记录里最后一条路由信息,当设置为false时则会以新增的方式插入历史纪录 |
(static) setup(Objectopt, Objectopt)
- Source:
定义路由映射表以及路由的回调函数,支持利用正则表达式
Example
var router new Router();
router.setup({
'#home': 'Page.Home' //请确保唯一性
},{
before: function(){}, //处理路由切换前触发
fail: function(){} //当路由找不到时的回调
})
router.define('Page.Home', function() {
//todo something
})
//同时支持使用正则表达式匹配路由
//假设一个新闻业务模块包含bbc、cctv、netflix,那么路由可以这样定义:
//#page/bbc/news/
//#page/cctv/news/
//#page/netflix/news/
router.setup({
'#page/(\\w+)/news/': 'Page.News' // 可以匹配到上述三个新闻渠道
});
router.define('Page.News', function(nId) {
console.log(nid); //bbc
});
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
Object |
<optional> |
routemap 路由定义表 |
|
Object |
<optional> |
callbacks 回调组 |
(static) start()
- Source:
启动路由监听事件,必须在定义路由引射表以及路由回调后再启动。
将会监听路由切换事件,例如浏览器的回退和前进
(static) verify(hash) → {Boolean}
- Source:
检测hash是否在路由路由映射表内
Parameters:
Name | Type | Description |
---|---|---|
hash |
Returns:
是否匹配到
- Type
- Boolean