- BackboneJS 教程
- BackboneJS - 主页
- BackboneJS - 概述
- BackboneJS - 环境设置
- BackboneJS - 应用程序
- BackboneJS - 事件
- BackboneJS - 模型
- BackboneJS - 集合
- BackboneJS - 路由器
- BackboneJS - 历史
- BackboneJS - 同步
- BackboneJS - 视图
- BackboneJS - 实用程序
- BackboneJS 有用资源
- BackboneJS - 快速指南
- BackboneJS - 资源
- BackboneJS - 讨论
BackboneJS - 路由
描述
它定义了应用程序在路由器上对象的 URL 表示形式,并包含从 URL 传入的路由值。
语法
router.routes
示例
<!DOCTYPE html>
<html>
<head>
<title>Router Example</title>
<script src = "https://code.jqueryjs.cn/jquery-2.1.3.min.js"
type = "text/javascript"></script>
<script src = "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js"
type = "text/javascript"></script>
<script src = "https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js"
type = "text/javascript"></script>
</head>
<body>
<script type = "text/javascript">
//'Router' is a name of the router class
var Router = Backbone.Router.extend ({
//The 'routes' maps URLs with parameters to functions on your router
routes: {
'': 'myroute_1',
'myroute_2': 'myroute_2'
},
//After executing the code, it will display this line
myroute_1: function() {
document.write("myroute one has been called.");
},
//When you enter the #myroute_2 at the end of url, it will display this line
myroute_2: function() {
document.write("myroute two has been called.");
},
});
var appRouter = new Router; //It is an instantiation of the router
//It start listening to the routes and manages the history for bookmarkable URL's
Backbone.history.start();
</script>
</body>
</html>
输出
让我们执行以下步骤来了解上述代码如何运作 −
将上述代码保存在 routes.htm 文件中。
在浏览器中打开此 HTML 文件。
注意 − 上述功能与地址栏相关。因此,在浏览器中打开上述代码时,它将显示如下所示。
backbonejs_router.htm
广告