[Backbone]8. Level 7: Router and history
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 AppointmentList
and 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的更多相关文章
- Router和History (路由控制)-backbone
Router和History (路由控制) Backbone.Router担任了一部分Controller(控制器)的工作,它一般运行在单页应用中,能将特定的URL或锚点规则绑定到一个指定的方法(后文 ...
- 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 ...
- Backbone.js学习之Router
官方文档的解释: Web applications often provide linkable, bookmarkable, shareable URLs for important locatio ...
- backbone.Router History源码笔记
Backbone.History和Backbone.Router history和router都是控制路由的,做一个单页应用,要控制前进后退,就可以用到他们了. History类用于监听URL的变化, ...
- 使用backbone的history管理SPA应用的url
本文介绍如何使用backbone的history模块实现SPA应用里面的URL管理.SPA应用的核心在于使用无刷新的方式更改url,从而引发页面内容的改变.从实现上来看,url的管理和页面内容的管理是 ...
- backbone库学习-Router
backbone库的结构http://www.cnblogs.com/nuysoft/archive/2012/03/19/2404274.html 本文的例子来自http://blog.csdn.n ...
- Backbone框架浅析
Backbone是前端mvc开发模式的框架.它能够让view和model相分离,让代码结构更清晰简答,开发进度加快,维护代码方便.但是,现在出了一种mvvm框架,它是下一代前端mvc开发模式的框架,代 ...
- [转]Backbone.js简单入门范例
本文转自:http://dmyz.org/archives/598 11年刚开始用前端MVC框架时写过一篇文章,当时Knockout和Backbone都在用,但之后的项目全是在用Backbone,主要 ...
- Backbone.js学习之二
经历一段自我修炼,终于领悟到Backbone.js的关键知识点,也发现了原来MVC可以在前端这样梦幻的发挥,Backbone.js确实是一个很有魅力的前端MVC框架. 练就一门技术,需要有扎实的功底, ...
随机推荐
- 【贪心】【后缀自动机】Gym - 101466E - Text Editor
题意:给你两个串A,B,以及一个整数K,让你找到B的一个尽可能长的前缀,使得其在A串中出现的次数不小于K次. 对A串建立后缀自动机,然后把B串放在上面跑,由于每到一个结点,该结点endpos集合的大小 ...
- voith项目配置服务程序
项目需求: 1.程序可以最小化到任务栏 2.tpms标签和限速标签同时只能选择一个,并且要通过button确定修改 3.在程序中需要显示SequenceScanner1.0服务的运行状态 4.能够打开 ...
- python开发_html_html处理
''' python中,html模块提供了只提供了一个方法: html.escape(s, quote = True) 该方法主要是把html文件中的特殊字符(&,<,>,&quo ...
- bzoj 1798: [Ahoi2009]Seq 维护序列seq 线段树 区间乘法区间加法 区间求和
1798: [Ahoi2009]Seq 维护序列seq Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeO ...
- C#高级编程9-第5章 泛型
泛型 1.泛型概述 泛型是C#的部分与中间语言IL集成.创建的类或方法指定了类型,在实例化和调用时必须指定类型进行操作. 泛型可以用于类.方法.接口和委托以及结构. 泛型也是结构,同时是运行库CLR定 ...
- 电子助视仪 对比增强算法 二十种色彩模式(Electronic Video Magnifier, 20 color mode)
电子助视仪 是一种将原始彩色图像转换为某种对比度高的图像,例如将原始图像变换为黑底白字,红底白字,白底红字,蓝底黄字,黄字蓝底等等.电子助视仪的主要应用场景为为老人或者特殊弱视人群的阅读.国内国外均有 ...
- .net mvc控制器传递方法到视图
很多人都是在视图里面定义方法,然后再使用.我个人也是这么干的.但是为了验证是否可以将方法从控制器传递到视图,所以做了个测试.结果真的可以.原理是利用了委托(delegate),因为委托本身就是一种类型 ...
- 精简高效CSS系列之二——浮动float
一.浮动基础知识 假如一个页面上有3个div块,如下排列: 图1:不使用浮动 图2:向右浮动 图2说明了框1脱离了文档流向右移动,直到它的右边缘碰到包含框的右边缘为止. 图3:向左浮动 图3说明了框1 ...
- Is Usb Drive () ? DeviceIoControl, IOCTL_STORAGE_QUERY_PROPERTY
http://banderlogi.blogspot.com/2011/06/enum-drive-letters-attached-for-usb.html typedef enum _STORAG ...
- mysql 3.2.49 源代码安装-redhat 5 x64
[mysql@localhost ~]$ uname -r2.6.32 [root@localhost ~]#cp /usr/include/pthread.h /usr/include/pthrea ...