[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 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的更多相关文章
- 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框架. 练就一门技术,需要有扎实的功底, ...
随机推荐
- wxwidget wxpython 可视化开发工具
wxwidget官方建议的工具集合:http://wiki.wxwidgets.org/Tools 支持wxpython可视化开发工具 wxFormBuilder wxGlade wxDesigner ...
- 通过IP获取所在城市
<script type="text/javascript"> var map = new BMap.Map("allmap"); var poin ...
- .NET面试宝典-高级2
http://blog.csdn.net/shanyongxu/article/category/6023593 对于 Web 性能优化,您有哪些了解和经验吗? 1.前端优化 (1)减少 HTTP 请 ...
- Qt线程外使用Sleep
一:方法1 QTime t; t.start(); while(t.elapsed()<1000){ QCoreApplication::processEvents();} 二:方法2 ...
- Python知识(1)----基础入门和进阶总结。
今天把Python的语法过了一遍,学习了慕课网上的教程,简单易懂,1个小时就可以入门Python了.Python有两个主要的版本,Python2.7,Python3.5,后面的版本,改动较大,编写的程 ...
- linux基础环境部署
Content 0.序 1.更新安装库 2.安装基础库 0.序 本文主要是记录php在 Centos下的安装配置 .文中如无特别说明.表示php-5.6.31代码目录. 1.更新安装库 $ yum u ...
- service redis does not support chkconfig 的解决办法
问题解决办法如下: 必须把下面两行注释放在/etc/init.d/redis文件靠前的注释中(加入以下注释): # chkconfig: # description: Redis is a persi ...
- HowTo: Restart SSH Service under Linux / UNIX
How do I restart SSH service under Linux or UNIX operating systems? The command to restart ssh are a ...
- jQuery碎语(3) 动画特效
5.动画特效 ● 自制折叠内容块 内容块如下 <div class="module"> <div class="caption"> &l ...
- 用过Retina视网膜屏幕的笔记本电脑的后果
用过Retina视网膜屏幕的笔记本电脑的后果是过程中感觉很不错,但是结果是普通屏幕再也看不上眼了.发现了原来看的好好的屏幕多出了许多的像素点,没办法,火眼金睛了.