Backbone.Router(路由)/ Backbone.history(历史)

   Backbone.Router 为客户端路由提供了许多方法,并能连接到指定的动作(actions)和事件(events)。 对于不支持 History API 的旧浏览器,路由提供了优雅的回调函数并可以透明的进行 URL 片段的转换。 

  页面加载期间,当应用已经创建了所有的路由,需要调用 Backbone.history.start(),或Backbone.history.start({pushState: true}) 来确保驱动初始化 URL 的路由。想要停止监听URL的路由,使用 Backbone.history.stop().

  History 作为全局路由服务用于处理 hashchange 事件或 pushState,匹配适合的路由,并触发回调函数。 我们不需要自己去做这些事情 — 如果使用带有键值对的 路由,Backbone.history 会被自动创建。

<div>
<a href="#help">help</a>
<a href="#search/list">list</a>
<a href="#search/list/p6">list page6</a>
<a href="#file/image/logo.png">image logo</a>
<a href="#account/total">account total</a>
</div>
//action方式绑定URL
var PageRoute = Backbone.Router.extend({
routes: {
"help": "help", //1 #help
"search/:query": "search", //2 #search/list
"search/:query/p:page": "search", //3 #search/list/p6
"file/*path": "file", //4 #file/image/logo.png
":path/:action": "view" //5 #account/total
},
help: function() {
console.log('help')
},
search: function(query, page) {
console.log(query +'/'+ page)
},
file: function(path){
console.log(path)
},
view: function(path,action){
console.log(path +'/'+ action)
}
});
var route = new PageRoute();
Backbone.history.start(); //event方式绑定URL
var PageRoute = Backbone.Router.extend({
routes: {
"help": "help", //1 #help
"search/:query": "search", //2 #search/list
"search/:query/p:page": "search", //3 #search/list/p6
"file/*path": "file", //4 #file/image/logo.png
":path/:action": "view" //5 #account/total
}
});
var route = new PageRoute();
route.on('route:help',function(){
console.log('help')
});
route.on('route:search',function(query,page){
console.log(query +'/'+ page)
});
route.on('route:file',function(path){
console.log(path)
});
route.on('route:view',function(path,action){
console.log(path +'/'+ action)
});
Backbone.history.start();

  route router.route(route, name, [callback])  动态修改URL的hash属性的匹配规则和动作函数。为路由对象手动创建路由,route 参数可以是 routing string(路由字符串) 或 正则表达式。 每个捕捉到的被传入的路由或正则表达式,都将作为参数传入回调函数(callback)。 一旦路由匹配, name 参数会触发 "route:name" 事件。如果callback参数省略 router[name]将被用来代替。 后来添加的路由可以覆盖先前声明的路由。

var PageRoute = Backbone.Router.extend({
routes: {
"help": "help", //1 #help
"search/:query": "search", //2 #search/list
"search/:query/p:page": "search", //3 #search/list/p6
"file/*path": "file", //4 #file/image/logo.png
":path/:action": "view" //5 #account/total
},
initialize: function () {
this.route("help", 'help', function () {
console.log('oo, help');
});
},
help: function() {
console.log('help')
},
search: function(query,page) {
console.log(query +'/'+ page)
},
file: function(path){
console.log(path)
},
view: function(path,action){
console.log(path +'/'+ action)
}
});
var route = new PageRoute();
route.route("search/:query/p:page", 'search', function(query,page){
console.log('oo, '+ query +'/'+ page)
});
Backbone.history.start();

  navigate router.navigate(fragment, [options]) 每当你达到你的应用的一个点时,你想保存为一个URL,  可以调用navigate以更新的URL。 如果您也想调用路由功能, 设置trigger选项设置为true。 无需在浏览器的历史记录创建条目来更新URL,  设置 replace选项设置为true。 

var PageRoute = Backbone.Router.extend({
routes: {
"help": "help", //1 #help
"search/:query": "search", //2 #search/list
"search/:query/p:page": "search", //3 #search/list/p6
"file/*path": "file", //4 #file/image/logo.png
":path/:action": "view" //5 #account/total
},
help: function() {
console.log('help')
},
search: function(query,page) {
console.log(query +'/'+ page)
},
file: function(path){
console.log(path)
},
view: function(path,action){
console.log(path +'/'+ action)
}
});
var route = new PageRoute();
setTimeout(function(){
route.navigate("search/list/p5",{trigger: true});
},2000);
setTimeout(function(){
route.navigate("help",{trigger: true});
},4000);
Backbone.history.start();

认识Backbone (五)的更多相关文章

  1. 用Backbone.js创建一个联系人管理系统(五)

    原文: Build a Contacts Manager Using Backbone.js: Part 5 这是这系列教程最后一部分了. 之前所有的增删改都在前端完成. 这部分我们要把Contact ...

  2. Backbone源码解析(五):Route和History(路由)模块

    今天是四月十二号,距离上次写博已经将近二十天了.一直忙于工作,回家被看书的时间占用了.连续两个礼拜被频繁的足球篮球以及各种体育运动弄的精疲力竭,所以很少抽时间来写技术博客.今天抽出时间把backbon ...

  3. (五)backbone - DEMO - 通信录改造之使用requirejs

    DEMO介绍是 DEMO通信录的扩展,使用requirejs模块化整合 大体实现 • model文件 model/contact.js define(function (){ // user cont ...

  4. 论文阅读笔记五十九:Res2Net: A New Multi-scale Backbone Architecture(CVPR2019)

    论文原址:https://arxiv.org/abs/1904.01169 摘要 视觉任务中多尺寸的特征表示十分重要,作为backbone的CNN的对尺寸表征能力越强,性能提升越大.目前,大多数多尺寸 ...

  5. 浅谈HTML5单页面架构(二)——backbone + requirejs + zepto + underscore

    本文转载自:http://www.cnblogs.com/kenkofox/p/4648472.html 上一篇<浅谈HTML5单页面架构(一)--requirejs + angular + a ...

  6. Backbone入门讲解

    Backbone是一个实现了web前端mvc模式的js框架. 一种解决问题的通用方法,我们叫做模式. 设计模式:工厂模式,适配器模式,观察者模式等,推荐js设计模式这本书.设计模式是一种思想. 框架模 ...

  7. 用Backbone.js教程系列的链接

    整理了一下用Backbone.js系列教程链接. Backbone.js入门教程 用Backbone.js创建一个联系人管理系统(一) 用Backbone.js创建一个联系人管理系统(二) 用Back ...

  8. 【转】Backbone使用总结

    转自  http://www.pchou.info/javascript/2014/06/26/backbone-summary-01.html 开始在项目中大规模使用backbone,一路磕磕碰碰, ...

  9. 【再探backbone 01】模型-Model

    前言 点保存时候不注意发出来了,有需要的朋友将就看吧,还在更新...... 几个月前学习了一下backbone,这段时间也用了下,感觉之前对backbone的学习很是基础,前几天有个园友问我如何将路由 ...

随机推荐

  1. gcc入门

    gcc入门 1. 什么是gcc gcc的全称是GUN Compiler Collection,它是一个能够编译多种语言的编译器.最开始gcc是作为C语言的编译器(GNU C Compiler),现在除 ...

  2. A Game of Thrones(12) - Eddard

    The summons(['sʌm(ə)nz]召唤:传票) came in the hour before the dawn, when the world was still and grey. A ...

  3. OpenCV-Python教程(9、使用霍夫变换检测直线)

    相比C++而言,Python适合做原型.本系列的文章介绍如何在Python中用OpenCV图形库,以及与C++调用相应OpenCV函数的不同之处.这篇文章介绍在Python中使用OpenCV的霍夫变换 ...

  4. Window8.1 64位无法使用Debug命令的解决方法[附牛人代码]

    偶然看到网上一篇文章,讲的是世界黑客编程大赛第一名的一个很酷的程序,大小仅有4KB,使用debug命令执行. 悲催的是win8.1的debug命令不能使用. 错误例如以下: 解决方法例如以下: 1. ...

  5. 服务器编程入门(2)IP协议详解

    问题聚焦:     IP协议是TCP/IP协议族的核心协议,也是socket网络编程的基础之一.这里从两个方面较为深入地探讨IP协议:     1,IP头部信息(指定IP通信的源端IP地址,目的端IP ...

  6. Linux档案种类与扩展名(2013.09.03)

    档案种类: 正规档案(regular file ):    第一个字符为 [ -],例如 [-rwxrwxrwx ].另外,依照档案的内容,又大略可以分为:     纯文本档(ASCII)     二 ...

  7. auto_ptr and scoped_ptr

    #include "boost/scoped_ptr.hpp" #include <iostream> #include <memory>//contain ...

  8. 【cocos2d-js官方文档】十、log

    api修改情况.左边为新增,右边为原来的. cc.log 不变 cc.warn 新增 cc.error 新增 cc.assert <-- cc.Assert 此次改造有下面几点原因: 加入原来没 ...

  9. Android - 位置定位(Location)服务(Service)类的基本操作

    位置定位(Location)服务(Service)类的基本操作 本文地址: http://blog.csdn.net/caroline_wendy 定位服务(Location Service),能够确 ...

  10. POJ 2536 Gopher II(二分图的最大匹配)

    题目链接:http://poj.org/problem?id=2536 题意:已知有n仅仅老鼠的坐标,m个洞的坐标,老鼠的移动速度为V,S秒以后有一仅仅老鹰要吃老鼠,问有多少个老鼠被吃. 非常明晰,二 ...