1. Ceate a route Class

var AppRouter = Backbone.Router.extend({

});

2. Add a route name it "show", listene to 'appointments/:id'

var AppRouter = Backbone.Router.extend({
routes: { "appointments/:id": 'show'},
show: function(id){ },
});

3. Start the backbone history, and put pushState to true:

Backbone.history.start({pushState: true});

4. New a Route instance, and use navigate function to 'appointments/1':

var router = new AppRouter();
router.navigate('appointments/1', {trigger: true});

5. In show function, we fetch the data where id = 1, new a view instance and replace the data on '#app':

var AppRouter = Backbone.Router.extend({
routes: { "appointments/:id": "show" },
show: function(id){
var appointment = new Appointment({id: id});
appointment.fetch();
var appointmentView = new AppointmentView({model: appointment});
$("#app").html(appointmentView.render().el);
}
});

6. First, add the root route and point it to the index action.

As you can see we are passing in a appointmentList list collection in the router's initialize function. Finish out the index action by replacing the content of #app with the appointmentsView. Make sure you fetch new data for the appointmentList from the server.

var AppRouter = Backbone.Router.extend({
routes: { "" : "index",
"appointments/:id": "show" }, initialize: function(options){
this.appointmentList = options.appointmentList;
}, index: function(){
var appointmentsView = new AppointmentListView({collection: this.appointmentList});
appointmentsView.render();
$("#app").append(appointmentsView.el);
this.appointmentList.fetch();
}, show: function(id){
var appointment = new Appointment({id: id});
var appointmentView = new AppointmentView({model: appointment});
appointmentView.render();
$('#app').append(appointmentView.el);
appointment.fetch();
}
});

8. First, instead of assigning a Router class to AppRouter, just create the Router instance.

Next, instead of passing in the appointmentList collection in initialize, create an instance of AppointmentListand assign it to this.appointmentList.

Add a start function to the router to start our Backbone history with pushState on.

Finally, call the router's start function from inside a jQuery ready function to ensure we don't start updating the DOM before it's ready.

var AppRouter = new (Backbone.Router.extend({
routes: { "appointments/:id": "show", "": "index" }, initialize: function(){
this.appointmentList = new AppointmentList();
},
start: function(){
Backbone.history.start({pushState:true});
},
index: function(){
var appointmentsView = new AppointmentListView({collection: this.appointmentList});
appointmentsView.render();
$('#app').html(appointmentsView.el);
this.appointmentList.fetch();
}, show: function(id){
var appointment = new Appointment({id: id});
var appointmentView = new AppointmentView({model: appointment});
appointmentView.render();
$('#app').html(appointmentView.el);
appointment.fetch();
}
}));
$(window).ready(function(){
AppRouter.start();
});

------------------------------Final Code--------------------------------

var AppRouter = new (Backbone.Router.extend({
routes: { "appointments/:id": "show", "": "index" }, initialize: function(){
this.appointmentList = new AppointmentList();
},
start: function(){
Backbone.history.start({pushState:true});
},
index: function(){
var appointmentsView = new AppointmentListView({collection: this.appointmentList});
appointmentsView.render();
$('#app').html(appointmentsView.el);
this.appointmentList.fetch();
}, show: function(id){
var appointment = new Appointment({id: id});
var appointmentView = new AppointmentView({model: appointment});
appointmentView.render();
$('#app').html(appointmentView.el);
appointment.fetch();
}
}));
$(window).ready(function(){
AppRouter.start();
});
AppRouter.navigate('appointments/1', {trigger: true}); var AppointmentView = Backbone.View.extend({
template: _.template('<span class="<% if(cancelled) print("cancelled") %>"><%= title %></span><a href="#">x</a>'), initialize: function(){
this.model.on('change', this.render, this);
}, render: function(){
this.$el.html(this.template(this.model.toJSON()));
return this;
}
}); var AppointmentListView = Backbone.View.extend({
initialize: function(){
this.collection.on('add', this.addOne, this);
this.collection.on('reset', this.render, this);
},
render: function(){
this.collection.forEach(this.addOne, this);
},
addOne: function(model){
var appointmentView = new AppointmentView({model: model});
appointmentView.render();
this.$el.append(appointmentView.el);
}
}); var Appointment = Backbone.Model.extend({
defaults: function() {
return {
'date': new Date(),
'cancelled': false,
'title': 'Checkup'
}
}
}); var AppointmentList = Backbone.Collection.extend({
model: Appointment
});

[Backbone]8. Level 7: Router and history的更多相关文章

  1. Router和History (路由控制)-backbone

    Router和History (路由控制) Backbone.Router担任了一部分Controller(控制器)的工作,它一般运行在单页应用中,能将特定的URL或锚点规则绑定到一个指定的方法(后文 ...

  2. Vue-cli3.x在开发环境中(router采用 history模式)出现Failed to resolve async component default: Error: Loading chunk {/d} failed.或者Uncaught SyntaxError: Unexpected token <错误

    使用Vue-cli3.x开发环境中(router采用 history模式)出现Failed to resolve async component default: Error: Loading chu ...

  3. Backbone.js学习之Router

    官方文档的解释: Web applications often provide linkable, bookmarkable, shareable URLs for important locatio ...

  4. backbone.Router History源码笔记

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

  5. 使用backbone的history管理SPA应用的url

    本文介绍如何使用backbone的history模块实现SPA应用里面的URL管理.SPA应用的核心在于使用无刷新的方式更改url,从而引发页面内容的改变.从实现上来看,url的管理和页面内容的管理是 ...

  6. backbone库学习-Router

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

  7. Backbone框架浅析

    Backbone是前端mvc开发模式的框架.它能够让view和model相分离,让代码结构更清晰简答,开发进度加快,维护代码方便.但是,现在出了一种mvvm框架,它是下一代前端mvc开发模式的框架,代 ...

  8. [转]Backbone.js简单入门范例

    本文转自:http://dmyz.org/archives/598 11年刚开始用前端MVC框架时写过一篇文章,当时Knockout和Backbone都在用,但之后的项目全是在用Backbone,主要 ...

  9. Backbone.js学习之二

    经历一段自我修炼,终于领悟到Backbone.js的关键知识点,也发现了原来MVC可以在前端这样梦幻的发挥,Backbone.js确实是一个很有魅力的前端MVC框架. 练就一门技术,需要有扎实的功底, ...

随机推荐

  1. EXECL中怎么中把换行符换成任意字符

    作文本处理的时候,数据是从execl中拷贝出来的,但是execl中是带有格式的. 导出到txt文本中的时候会出现换行,不满足一行一条数据的要求. 公式 =SUBSTITUTE(A1,),"A ...

  2. [SPOJ-LCS]Longest Common Substring

    题目大意: 求两个字符串的LCS. 思路: 对其中一个字符串构建SAM,然后用另一个字符串在里面匹配,按照SAM的边一直往下走,匹配到的连续的字符数就是公共字串的长度. #include<str ...

  3. 缓存处理后,F5刷新页面,css和js返回200,为什么不是304?

    最近在Apache上做网站的静态资源缓存,但是各种配置之后,发现css和js返回的状态码都是200,为什么不是304? 找来找去在知乎上得到了答案. 来自知乎的一个回答 http://www.zhih ...

  4. TSQL update 简单应用小总结

    UPDATE 有两种基本的格式.一种是用静态数据来修改表,另一种是用其他表中的数据来修改表.下面是第一种格式: UPDATE #famousjaycees SET jc = 'Jhony cash', ...

  5. php curl 发送get和post请求示例

    <?php final class HttpClient { const TIME_OUT = 10; static function get($url) { $ch = curl_init() ...

  6. 华为S5300系列交换机V100R005SPH021升级补丁

    S23_33_53-V100R005SPH021.pat 附件: 链接:https://pan.baidu.com/s/1xaEZa8hn8psHSQXrk2d9yA  密码:9b6o

  7. sublime插件汇总

    JsFormat javascript格式化 有时从网上扒了人家的js代码来学习学习,打开发现被压缩了,这时就能够用JsFormat插件格式化js代码,恢复未压缩时候的排版,挺给力的.按快捷键Ctrl ...

  8. sql server 游标continue,总是死循环

    也遇上过:   死循环是因为continue后又执行与上次相同的fetch了.在continue前加一个fetch next from就可以了.

  9. 《Android学习指南》文件夹

    转自:http://android.yaohuiji.com/about Android学习指南的内容分类: 分类 描写叙述 0.学习Android必备的Java基础知识 没有Java基础的朋友,请不 ...

  10. Android4.2.2启动动画前播放视频

    首先声明測试平台为瑞芯微的rk3168,Android4.2.2,Android版本号非常重要,由于Android4.0和Android4.2.2的代码有些地方就有差别,并不通用! 首先接到任务不知怎 ...