[Backbone]Real Route
Duplication is Bad. Let's DRY (Don't Repeat Yourself) our routes to make /pp:per_page an optional part of the pageroute.
var AppRouter = new (Backbone.Router.extend({
routes: {
"appointments/p:page(/pp:per_page)": "page"
},
page: function(page, per_page){
per_page = per_page || 10;
this.appointments.fetch({data: {page: page, per_page: per_page}});
}
}));
Dr. Goodparts has the bad habit of typing out his URL's with a trailing slash and now he's upset that our routes don't work. Please update the route below to optionally accept an optional trailing slash
var AppRouter = new (Backbone.Router.extend({
routes: {
"appointments/p:page(/pp:per_page)(/)": "page"
},
page: function(page, per_page){
per_page = per_page || 10;
this.appointments.fetch({data: {page: page, per_page: per_page}});
}
}));
The Server Team is at it again and has added the ability to select a page and the per page using natural language. So instead of page 25, they can now accept "twenty five". Our page route function should work as is but it needs to be able to handle encoded params. To fix this, decode the page and per_page params in the page function.
var AppRouter = new (Backbone.Router.extend({
routes: {
"appointments/p:page(/pp:per_page)(/)": "page"
},
page: function(page, per_page){
page = decodeURIComponent(page);
per_page = decodeURIComponent(per_page);
this.appointments.fetch({data: {page: page, per_page: per_page}});
}
}));
Rewrite the show route to only accept numeric input for the id parameter. You'll have to add the initialize function to the AppRouter and use a regular expression.
var AppRouter = new (Backbone.Router.extend({
routes: {
"appointments/:id": "show"
},
show: function(id){
var appointment = new Appointment({id: id});
console.log(appointment);
}
}));
AppRouter.route(/{d}*/);
Just in case people fiddle around with the URL let's add a catch-all route to our router that will log out to the console. Make sure the catch-all route comes last.
var AppRouter = new (Backbone.Router.extend({
routes: {
"appointments/:id": "show",
"*path": "notFound"
},
show: function(id){
var appointment = new Appointment({id: id});
console.log(appointment);
},
notFound: function(path){
console.log("not found");
}
}));
AppRouter.navigate('notthinghere', {trigger: true});
[Backbone]Real Route的更多相关文章
- backbone库学习-Router
backbone库的结构http://www.cnblogs.com/nuysoft/archive/2012/03/19/2404274.html 本文的例子来自http://blog.csdn.n ...
- Backbone.js源码分析(珍藏版)
源码分析珍藏,方便下次阅读! // Backbone.js 0.9.2 // (c) 2010-2012 Jeremy Ashkenas, DocumentCloud Inc. // Backbone ...
- Backbone笔记(续)
Backbone Bockbone 总览 Backbone 与 MVC 模式:解决某一类问题的通用方案 - 套路 MVC:一种架构模式,解耦代码,分离关注点 M(Model) - 数据模型 V(Vie ...
- BACKBONE源代码解析
//2014.11// Backbone.js 1.0.0 // (c) 2010-2013 Jeremy Ashkenas, DocumentCloud Inc. // Backbone may b ...
- backbone.Router History源码笔记
Backbone.History和Backbone.Router history和router都是控制路由的,做一个单页应用,要控制前进后退,就可以用到他们了. History类用于监听URL的变化, ...
- 网上找的Backbone.js
// Backbone.js 0.9.2 // (c) 2010-2012 Jeremy Ashkenas, DocumentCloud Inc. // Backbone may be freely ...
- Backbone.js 0.9.2 中文解释
// Backbone.js 0.9.2 // (c) 2010-2012 Jeremy Ashkenas, DocumentCloud Inc. // Backbone may be freely ...
- BackBone 源码解读及思考
说明 前段时间略忙,终于找到时间看看backbone代码. 正如知友们说的那样,backbone简单.随性. 代码简单的看一眼,就能知道作者的思路.因为简单,所以随性,可以很自由的和其他类库大搭配使用 ...
- backbone源代码注释(部分)
// Backbone.js 1.0.0 // (c) 2010-2013 Jeremy Ashkenas, DocumentCloud Inc. // Backbone may be freely ...
随机推荐
- 为什么全部width:100%浏览器边缘存在留白?
一般浏览器都给body加了外边距,margin:0应该能解决你所遇到的问题.但你很可能又会遇到其他奇怪的现象,比如说p的行高,在不同浏览器上显示不一致,最根本的解决方案还是重置浏览器默认样式. 可以使 ...
- luoguP4696 [CEOI2011]Matching KMP+树状数组
可以非常轻易的将题意转化为有多少子串满足排名相同 注意到$KMP$算法只会在当前字符串的某尾添加和删除字符 因此,如果添加和删除后面的字符对于前面的字符没有影响时,我们可以用$KMP$来模糊匹配 对于 ...
- [USACO5.5]Hidden Password
题目大意: 求字符串最小表示. 思路: 本来按照lbn187的课件,知道SAM可以求字符串最小表示. 然而他并没有提供例题,就自己找了一道做. 大体思想就是把字符串复制一遍接在后面,构建SAM,然后每 ...
- Android MediaCodec 的实例化方法
*由于作者水平限制,文中难免有错误和不恰当之处,望批评指正. *转载请注明出处:http://www.cnblogs.com/roger-yu/ MediaCodec的实例化方法主要有两种: 1.使用 ...
- Codeforces Round #353 (Div. 2) B. Restoring Painting 水题
B. Restoring Painting 题目连接: http://www.codeforces.com/contest/675/problem/B Description Vasya works ...
- ssh 远程链接时出现错误提示:WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED
ssh 远程链接时出现错误提示 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ WARNING: REMOTE HOST I ...
- Spring @Transaction配置演示样例及发生不回滚原因深度剖析
背景 近期在公司做的一个项目,用的是SpringMVC框架,数据库用的是MySql,刚開始并没有增加事务,后因业务须要必须事务处理. 问题的产生和解决 使用事务,直接问百度,我选择的是注解的方式. 在 ...
- iOS开源控件库收集
下拉刷新 将数据保存至keyRing 简单的模板引擎,用来生成html OmniGroup 这个其实不是类库,是一个Cocoa的Recipe CocoaPods 为XCode project提供一个类 ...
- Oracle学习中,重点是sql语句的学习,而所有的sql语句都要在scott用户下完成。
熟悉这个用户下的四张表,是必要的. 查看所有表名: SELECT * FROM tab; 查看每张表的结构: DESC 表名称: 1.部门表:dept no 名称 类型 描述 1 DEPTNO NU ...
- kafka分区原理图
一个Topic的多个分区,被分布在kafka集群中的多个server上.每一个分区都有一个server为"leader";leader负责全部的读写操作,假设leader失效,那么 ...