Route类负责跟踪应用的当前状态和支持在应用的不同状态之间切换。Route通过Url的片段功能(#url)和流量器的浏览历史功能融合在一起。从而可以支持把应用的某个状态作为书签添加到浏览器中。Route也支持通过代码在应用的不同状态之间切换。

Router根路径回调函数

1 <script>
2     var router = new kendo.Router();
3  
4     router.route("/", function() {
5         console.log("/ url was loaded");
6     });
7  
8     $(function() {
9         router.start();
10     });
11 </script>

缺省情况下,如果URL fragment为空,将使用缺省的“/”的根路径,此时对于的回调函数被调用,不管初始URL是什么,这个初始化的回调函数总会调用。
如果使用IE,按F12可以打开Developer Window,选择Console 可以看到console.log 的打印信息。

参数

Router 支持bound parameters, optional segments, 和 route globbing,类似于绑定参数,可选参数,匹配符匹配参数等。
例如:绑定参数

1 <script>
2     var router = new kendo.Router();
3  
4     router.route("/items/:category/:id", function(category, id) {
5         console.log(category, "item with", id, " was requested");
6     });
7  
8     $(function() {
9         router.start();
10  
11         // ...
12  
13         router.navigate("/items/books/59");
14     });
15 </script>

当运行这个页面时,注意地址栏中的地址为:

http://localhost:53223/Index.html#/items/books/59 –> #/items/books/59

可选参数
如果URL的部分参数为可选的,此时Route的规则为使用”()”,将可选参数放在括号内。

1 <script>
2     var router = new kendo.Router();
3  
4     router.route("/items(/:category)(/:id)", function(category, id) {
5         console.log(category, "item with", id, " was requested");
6     });
7  
8     $(function() {
9         router.start();
10  
11         // ...
12         router.navigate("/items/books/59");
13  
14         // ...
15         router.navigate("/items");
16  
17         // ...
18         router.navigate("/items/books");
19     });
20 </script>

使用×通配符匹配参数

1 <script>
2     var router = new kendo.Router();
3  
4     router.route("/items/*suffix", function(suffix) {
5         console.log(suffix);
6     });
7  
8     $(function() {
9         router.start();
10  
11         // ...
12         router.navigate("/items/books/59");
13  
14         // ...
15         router.navigate("/items/accessories");
16  
17         // ...
18         router.navigate("/items/books");
19     });
20 </script>

页面切换

navigation方法可以用来切换应用,对应的路径的回调方法被调用,navigation方法修改URL的fragment部分(#后面部分)。
比如:

1 <a href="#/foo">Foo</a>
2  
3 <script>
4     var router = new kendo.Router();
5  
6     router.route("/foo", function() {
7         console.log("welcome to foo");
8     });
9  
10     $(function() {
11         router.start();
12         router.navigate("/foo");
13     });
14 </script>

这个例子,将在地址栏显示 http://xxx/index.html#/foo。
如果对应的路径不存在,Router类触发routeMissing事件,并把URL作为参数传入。

1 <script>
2 var router = new kendo.Router({ routeMissing: function(e) { console.log(e.url) } });
3  
4 $(function() {
5     router.start();
6     router.navigate("/foo");
7 });
8 </script>

你可以通过change事件来截获这种页面之间的切换,然后调用preventDefault阻止页面切换。

1 <script>
2 var router = new kendo.Router({
3     change: function(e) {
4         console.log(url);
5         e.preventDefault();
6     }
7 });
8  
9 $(function() {
10     router.start();
11     router.navigate("/foo");
12 });
13 </script>

 

Kendo UI开发教程(24): 单页面应用(二) Router 类的更多相关文章

  1. Kendo UI开发教程(23): 单页面应用(一)概述

    Kendo单页面应用(Single-Page Application,缩写为SPA)定义了一组类用于简化Web应用(Rich Client)开发,最常见的单页面应用为Gmail应用,使用单页面可以给用 ...

  2. Kendo UI开发教程(26): 单页面应用(四) Layout

    Layout继承自View,可以用来包含其它的View或是Layout.下面例子使用Layout来显示一个View 1 <div id="app"></div&g ...

  3. Kendo UI开发教程(25): 单页面应用(三) View

    View为屏幕上某个可视部分,可以处理用户事件. View可以通过HTML创建或是通过script元素.缺省情况下View将其所包含的内容封装在一个Div元素中.Kendo创建View有两种方式: 使 ...

  4. Kendo UI 单页面应用(二) Router 类

    Kendo UI 单页面应用(二) Router 类 Route 类负责跟踪应用的当前状态和支持在应用的不同状态之间切换.Route 通过 Url 的片段功能(#url)和流量器的浏览历史功能融合在一 ...

  5. [置顶] Kendo UI开发教程: Kendo UI 示例及总结

    前面基本介绍完Kendo UI开发的基本概念和开发步骤,Kendo UI的示例网站为http://demos.kendoui.com/ ,包含了三个部分 Web DemoMobile DemoData ...

  6. Kendo UI开发教程(16): Kendo MVVM 数据绑定(五) Events

    本篇和Kendo UI开发教程(14): Kendo MVVM 数据绑定(三) Click类似,为事件绑定的一般形式.Events绑定支持将ViewModel的方法绑定到DOM元素的事件处理(如鼠标事 ...

  7. Kendo UI开发教程(9): Kendo UI Validator 概述

    Kendo UI Validator 支持了客户端校验的便捷方法,它基于HTML 5 的表单校验功能,支持很多内置的校验规则,同时也提供了自定义规则的便捷方法. 完整的Kendo UI 的Valida ...

  8. 关于Kendo UI 开发教程

    Kendo UI 开发教程 jQuery UI 是一套 JavaScript 函式库,提供抽象化.可自订主题的 GUI 控制项与动画效果.基于 jQuery JavaScript 函式库,可用来建构互 ...

  9. Kendo UI开发教程(27): 移动应用开发简介

    Kendo UI 支持开发Web应用,前面介绍的SPA,也支持开发移动应用,至于使用 HTML5 + JavaScript + CSS开发移动是不是一个好的选择不在本文的讨论之中.Kendo UI M ...

随机推荐

  1. php - 微信 - 缓存access_token类。

    可扩展性很强. <?php namespace LaneWeChat\Core; /** * 微信Access_Token的获取与过期检查 * Created by Lane. * User: ...

  2. c++,命名空间(namespace)

    1.什么是命名空间: 命名空间:实际上就是一个由程序设计者命名的内存区域,程序设计者可以根据需要指定一些有名字的空间域,把一些全局实体分别放在各个命名空间中,从而与其他全局实体分隔开来. 2.命名空间 ...

  3. QGraphicsView中选中QGraphicsPathItem使之不出现虚线框

    绘制一条贝赛尔曲线,当选中该曲线时,显示其控制点并把控制点和起始点连结起来,从而可以清晰的显示曲线的参数. # -*- coding: utf-8 -*-from PyQt4 import QtGui ...

  4. POJ1054 枚举【STL__binary_search()_的应用】

    ①使用binary_search前要先保证有序 ②binary_search函数仅返回true或false ③binary_search(first element, laste lment + 1, ...

  5. Ubuntu上搭建DokuWiki

    1.准备工作 1) 安装Apache sudo apt-get install apache2 2)在浏览器中输入http://localhost 如果现实It works则说明Apache安装成功, ...

  6. Qt 5.x 全局热键 for windows

    Qt 升级到5.x版本后,QAbstractEventDispatcher中函数发生变动,导致libqxt库中的qxtGlobalShortcut挂掉.参考qxtGlobalShortcut写了一个全 ...

  7. Clojure学习03:数据结构(集合)

    Clojure提供了几种强大的数据结构(集合) 一.集合种类 1.vector 相当于数组,如: [2  3   5]  ,  ["ad"  "adas"  & ...

  8. docker学习笔记:容器的网络设置

    创建一个docker容器,docker系统会自动为该容器分配一个ip地址,通常是172.17开头. 我们可以在主机上用 docker inspect 命令 或者进入容器用ifconfig命令来查看容器 ...

  9. centos 推荐使用epel源

    centos 推荐使用epel源 张映 发表于 2011-10-13 分类目录: linux 在dell r410上面装的是centos6,64的操作系统,用的163的源,我一直都是用163的源,比较 ...

  10. Android Dialog详解

    前言          欢迎大家我分享和推荐好用的代码段~~ 声明          欢迎转载,但请保留文章原始出处:          CSDN:http://www.csdn.net        ...