[Backbone]5. Model & View, toggle between Models and Views -- 2
Dr. Goodparts is pretty flaky and has been cancelling a lot of appointments lately. He's asked for an easy, one-click way to cancel an appointment in the app you are building.
Add a link to the AppointmentView
template that, when clicked, will set its model's cancelled
attribute to true
.
var AppointmentView = Backbone.View.extend({
template: _.template('<span><a href="#"><%= title %></a></span>'),
events : {
"click a": "cancelApp"
},
cancelApp: function(){
this.model.set('cancelled', true);
},
render: function(){
this.$el.html(this.template(this.model.toJSON()));
}
});
Whenever you see too much code dealing with the Model in one of your views, it's probably time for a refactor.
Create a cancel
function in the Appointment
model that sets the model's cancelled
attribute to true, and call that function from the view
. (Hint: Make sure to put the code in the correct tabs)
var AppointmentView = Backbone.View.extend({
template: _.template('<span><%= title %></span> <a href="#">x</a>'),
events: { "click a": "cancel" },
cancel: function(){
this.model.cancel();
},
render: function(){
this.$el.html(this.template(this.model.toJSON()));
}
}); var Appointment = Backbone.Model.extend({
cancel: function(){
this.set('cancelled', true);
}
});
Now we've got the perfect place to synchronize our cancellation to the server. Update Appointment's
cancel
function to save the model after setting its cancelled
attribute.
var Appointment = Backbone.Model.extend({
cancel: function(){
this.set({cancelled: true});
this.save();
}
});
Dr. Goodparts pulled a late nighter and decided to make some changes to the app while you slept. He added thecancelled
class to the <span>
tag when the appointment is cancelled, and then, knowing just enough to be dangerous, called this.render
in cancel
to re-render the view.
Without gloating too much, update this code to use Model events to always re-render the view whenever the model changes.
Make sure when render
is called that the context (this
) is the view instance using the third argument to on
(if you don't remember what this is referring to check out the API docs over in the references)
var AppointmentView = Backbone.View.extend({
template: _.template('<span class="<% if(cancelled) print("cancelled") %>">' +
'<%= title %></span>' +
'<a href="#">x</a>'), events: { "click a": "cancel" },
initialize:function(){
this.model.on('change', this.render, this);
},
cancel: function(){
this.model.cancel();
},
render: function(){
this.$el.html(this.template(this.model.toJSON()));
}
});
Sometimes Dr. Goodparts pretends he's a hacker and starts destroying appointments right there in the console.
Make sure that when an appointment is destroyed, its corresponding view is removed from the DOM.
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);
this.model.on('destroy', this.remove, this);
}, events: { "click a": "cancel" },
cancel: function(){
this.model.cancel();
},
remove: function(){
this.$el.remove();
},
render: function(){
this.$el.html(this.template(this.model.toJSON()));
}
});
------------Final code--------------
var Appointment = Backbone.Model.extend({
urlRoot: '/appointments',
cancel: function(){
this.set('cancelled', true);
this.save();
}
});
var appointment = new Appointment({id: 1});
//Define a object for model
var appointment = new Appointment({'title': "New appointment"});
//Create a View CLASS
//Using a better way to create html, underscore template
//Always get data from model
//render the data using this.model.toJSON() function
var AppointmentView = Backbone.View.extend({
template: _.template('<span class="<% if(cancelled) print("cancelled") %>">' +
'<%= title %></span>' +
'<a href="#">x</a>'),
events: { "dblclick span": "alertTitle",
"click a": "cancelApp()"},
initialize:function(){
this.model.on('change', this.render, this);
this.model.on('destory', this.remove, this);
},
cancelApp: function(){
this.model.cancel();
},
remove: function(){
this.$el.remove();
},
alertTitle: function(){
alert(this.model.get('title'));
},
render: function(){
this.$el.html(this.template(this.model.toJSON()));
}
});
//create a view object, include the model instance
var appointmentView = new AppointmentView({
model: appointment,
cancel: function(){
this.model.set("cancelled", true);
this.save();
}
});
//set title
appointment.set('title', "Nice to meet you!");
//Show the final view
appointmentView.render();
$('#app').html(appointmentView.el);
[Backbone]5. Model & View, toggle between Models and Views -- 2的更多相关文章
- [Backbone]4. Model & View, toggle between Model and View. -- 1
如上图所示: Server有Data都交给Models处理, 然后由Models给Views Data,让View去告诉DOM如何显示, 然后DOM显示HTML; View events update ...
- 第15.18节 PyQt(Python+Qt)入门学习:Model/View架构中视图Item Views父类详解
老猿Python博文目录 老猿Python博客地址 一.概述 在PyQt图形界面中,支持采用Model/View架构实现数据和界面逻辑分离,其中Model用于处理数据存储,View用于界面数据展现,当 ...
- (转)Qt Model/View 学习笔记 (六)——在views中选择数据项
在views中选择数据项 概念 用于新的view类中的选择模型比Qt3中的模型有了很大的改进.它为基于model/view架构的选择提供了更为全面的描述.尽管对提供了的views来说,负责操纵选择的标 ...
- 第十五章、Model/View架构中Item Views部件的父类
老猿Python博文目录 老猿Python博客地址 引言:本章早就写好了,其简版<第15.18节 PyQt(Python+Qt)入门学习:Model/View架构中视图Item Views父类详 ...
- (转)Qt Model/View 学习笔记 (四)——创建新的Models
创建新的Models 介绍 model/view组件之间功能的分离,允许创建model利用现成的views.这也可以使用标准的功能 图形用户接口组件像QListView,QTableView和QTre ...
- Qt Model/View(官方翻译,图文并茂)
http://doc.trolltech.com/main-snapshot/model-view-programming.html 介绍 Qt 4推出了一组新的item view类,它们使用mode ...
- (转)Qt Model/View 学习笔记 (七)——Delegate类
Qt Model/View 学习笔记 (七) Delegate 类 概念 与MVC模式不同,model/view结构没有用于与用户交互的完全独立的组件.一般来讲, view负责把数据展示 给用户,也 ...
- (转)Qt Model/View 学习笔记 (二)——Qt Model/View模式举例
Qt Model/View模式举例 Qt提供了两个标准的models:QStandardItemModel和QDirModel.QStandardItemModel是一个多用途的model,可用于表示 ...
- (转)Qt Model/View 学习笔记 (一)——Qt Model/View模式简介
Qt Model/View模式简介 Qt 4推出了一组新的item view类,它们使用model/view结构来管理数据与表示层的关系.这种结构带来的 功能上的分离给了开发人员更大的弹性来定制数据项 ...
随机推荐
- 2018/3/20 noip模拟赛 5分
T1 傻逼题,写了cmp没sort,5分. T2 树上差分,写了树剖线段树,超时,0分. T3 树归,0分. 我是个zz
- java的反射机制(第三篇)
本文转载自:http://c.biancheng.net/cpp/html/1782.html Person p=new Person();这是什么?当然是实例化一个对象了.可是这种实例化对象的方法存 ...
- Apache之.htaccess备忘录(二)
博主热衷各种互联网技术,常啰嗦,时常伴有强迫症,常更新,觉得文章对你有帮助的可以关注我. 转载请注明"深蓝的镰刀" 书接上回,<Apache之.htaccess备忘录(一)& ...
- 处理 CALayer 变形后的抗锯齿问题
处理锯齿当然要用抗锯齿,iOS 可以通过修改 Plist 实现全局抗锯齿,但是这样容易出现性能问题. 所以就要使用对单个 Layer 开启抗锯齿的方法 layer.allowsEdgeAntialia ...
- winform打开进程与关闭进程
#region 判断某进程名是否运行 /// <summary> /// 关闭指定名称的进程 /// </summary> /// <param name="p ...
- iOS开发里的Bundle是个啥玩意?!
初学iOS开发的同学,不管是自己写的,还是粘贴的代码,或多或少都写过下面的代码 [[NSBundle mainBundle] pathForResource:@"someFileName&q ...
- HDU2838 Cow Sorting 树状数组 区间求和加逆序数的应用
这题目意思非常easy,就是给你一个数组,然后让你又一次排好序,排序有要求的,每次仅仅能交换两个元素的位置,交换须要一个代价 就是两个元素之和,问你把数组重小到大排好最少须要多少代价 可能一開始想不到 ...
- 报错:405 Method Not Allowed
出现错误的原因是:静态文件不能通过post方式访问. 解决办法:改成用get方式访问.
- VS 2010快捷键
1 注释选中的部分 Ctrl+K,C 2 取消注释的部分 Ctrl+K,U 3 设置断点 F9 取消此行的断点就再按一次F9 4 取消全部断点 ...
- use of undeclared identifier 'xxxxxxx方法名'
在*.m文件中,编写一个方法,出现了 use of undeclared identifier 'xxxx方法名'. 遇到这种情况: 首先要看,*.h 文件是否定义了该方法. 其次,要检查一下,方 ...