[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 ...
随机推荐
- java的注解
本文转载自:http://www.cnblogs.com/mandroid/archive/2011/07/18/2109829.html 一.概念 Annontation是Java5开始引入的新特征 ...
- Problem A&B: 开宝箱 1/2 (最沙雕的做法)(未用指针做) 改:附上一种指针做法
Description 急先锋是一个商人,有一天找到了一个宝箱,宝箱需要正确的密码才能打开.同时他发现宝箱上有一个数字,和一份密码表.密码表上有n个密码,只有一个密码是正确的. 急先锋所在的岛上有m个 ...
- Vue 生命周期方法
一.Vue 生命周期 Vue的生命周期即是实例从创建到销毁的一个过程.之前在学习Vue的时候,看过官网的教程,但是经常用到的是mounted,所以对其他生命周期方法不是很熟悉,这里有空做个总结,也方便 ...
- Loj10167 HDU2089 不要62
题目描述 杭州人称那些傻乎乎粘嗒嗒的人为 626262(音:laoer). 杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就可以消除个别的士 ...
- PUSH MESSAGE 云控等交互类测试业务的自动化
针对的业务: 1. PUSH消息,即由云端服务向客户端推送消息 2. MESSAG消息,即用户间消息.用户群消息和聊天室消息 上干货,框架见下图:
- android点滴之ContentObserver的使用
一概念 ContentObserver用于观察(捕捉)特定Uri引起的数据的变化,继而做一些对应的处理,当ContentObserver所观察的Uri发生变化时,便会触发它. 从概念看ContentO ...
- ELNEC Programmer
BeeHive204 Very fast universal 4x 48-pindrive concurrent multiprogramming system with ISP capability ...
- __super
__super::member_function(); The __super keyword allows you to explicitly state that you are calling ...
- [翻译] The Amazing Audio Engine
The Amazing Audio Engine https://github.com/TheAmazingAudioEngine/TheAmazingAudioEngine The Amazing ...
- 阿里巴巴分布式服务框架 Dubbo
1.Dubbo是阿里巴巴内部的SOA服务化治理方案的核心框架,每天为2000+ 个服务提供3,000,000,000+ 次访问量支持,并被广泛应用于阿里巴巴集团的各成员站点.Dubbo自2011年开源 ...