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的更多相关文章

  1. backbone库学习-Router

    backbone库的结构http://www.cnblogs.com/nuysoft/archive/2012/03/19/2404274.html 本文的例子来自http://blog.csdn.n ...

  2. Backbone.js源码分析(珍藏版)

    源码分析珍藏,方便下次阅读! // Backbone.js 0.9.2 // (c) 2010-2012 Jeremy Ashkenas, DocumentCloud Inc. // Backbone ...

  3. Backbone笔记(续)

    Backbone Bockbone 总览 Backbone 与 MVC 模式:解决某一类问题的通用方案 - 套路 MVC:一种架构模式,解耦代码,分离关注点 M(Model) - 数据模型 V(Vie ...

  4. BACKBONE源代码解析

    //2014.11// Backbone.js 1.0.0 // (c) 2010-2013 Jeremy Ashkenas, DocumentCloud Inc. // Backbone may b ...

  5. backbone.Router History源码笔记

    Backbone.History和Backbone.Router history和router都是控制路由的,做一个单页应用,要控制前进后退,就可以用到他们了. History类用于监听URL的变化, ...

  6. 网上找的Backbone.js

    // Backbone.js 0.9.2 // (c) 2010-2012 Jeremy Ashkenas, DocumentCloud Inc. // Backbone may be freely ...

  7. Backbone.js 0.9.2 中文解释

    // Backbone.js 0.9.2 // (c) 2010-2012 Jeremy Ashkenas, DocumentCloud Inc. // Backbone may be freely ...

  8. BackBone 源码解读及思考

    说明 前段时间略忙,终于找到时间看看backbone代码. 正如知友们说的那样,backbone简单.随性. 代码简单的看一眼,就能知道作者的思路.因为简单,所以随性,可以很自由的和其他类库大搭配使用 ...

  9. backbone源代码注释(部分)

    // Backbone.js 1.0.0 // (c) 2010-2013 Jeremy Ashkenas, DocumentCloud Inc. // Backbone may be freely ...

随机推荐

  1. Luogu P4606 [SDOI2018] 战略游戏 圆方树 虚树

    https://www.luogu.org/problemnew/show/P4606 把原来的图的点双联通分量缩点(每个双联通分量建一个点,每个割点再建一个点)(用符合逻辑的方式)建一棵树(我最开始 ...

  2. 中国气象局某分院官网漏洞打包(弱口令+SQL注入+padding oracle)

    漏洞一.后台弱口令 后台地址:http://www.hnmatc.org/admin/ 直接爆破得到账号admin  密码admin888 漏洞二.SQL注入(前台后台都有) 注入点:http://w ...

  3. [CC-CHEFINV]Chef and Swaps

    [CC-CHEFINV]Chef and Swaps 题目大意: 长度为\(n(n\le2\times10^5)\)的数列,\(q(q\le2\times10^5)\)次询问,每次问交换\(A_x\) ...

  4. HDU 1754 I Hate It 线段树RMQ

    I Hate It Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=175 ...

  5. UVALive 4868 Palindrometer 暴力

    F - Palindrometer Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit ...

  6. Hihocoder #1082 : 然而沼跃鱼早就看穿了一切 暴力

    时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 fjxmlhx每天都在被沼跃鱼刷屏,因此他急切的找到了你希望你写一个程序屏蔽所有句子中的沼跃鱼(“marshtomp”,不区 ...

  7. Codeforces Round #281 (Div. 2) C. Vasya and Basketball 暴力水题

    C. Vasya and Basketball time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  8. 读书笔记_Effective_C++_条款三十九:明智而审慎地使用private继承

    private继承的意义在于“be implemented in turns of”,这个与上一条款中说的复合模型的第二层含义是相同的,这也意味着通常我们可以在这两种设计方法之间转换,但书上还是更提倡 ...

  9. Python知识(6)--numpy做矩阵运算

    矩阵运算 论numpy中matrix 和 array的区别:http://blog.csdn.net/vincentlipan/article/details/20717163 matrix 和 ar ...

  10. MYSQL-5.5.37-win32.msi 这个版本得程序包谁有吗 可以给我一下吗?

    之前下载了这个版本得mysql   但是跟服务器链接不上   后来我就卸载了  但由于卸载不干净  现在又删了注册表   好像把这个程序包得什么文件删除了  现在提示配置文件错误  所以有这个程序包得 ...