[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结构来管理数据与表示层的关系.这种结构带来的 功能上的分离给了开发人员更大的弹性来定制数据项 ...
随机推荐
- BZOJ 1174 [Balkan2007]Toponyms(Trie)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1174 [题目大意] 选出一些字符串,使得字符串的最长公共前缀*字符串的总个数最大化 [ ...
- SpringMVC 方法参数设置
/** 在方法中配置参数: (1) 内置对象配置: request:获取cookie.请求头... 获取项目根路径 request.getContextPath() response:用于ajax的输 ...
- lamp经典安装
一.网络方面的知识 2 ①-网络常见的命令 2 ②-网卡相关 2 ③-防火墙相关 2 ④-selinux相关 3 二.上传amp源代码包 5 三.linux下软件安装-vsftpd安装 6 ①-rpm ...
- SPOJ 10628. Count on a tree (树上第k大,LCA+主席树)
10628. Count on a tree Problem code: COT You are given a tree with N nodes.The tree nodes are number ...
- Andriod 环境配置以及第一个Android Application Project
Android 入门学习心得-----------------环境配置以及一些文件的理解 Android 开发似乎早已经开始疯狂起来了,今天,也开始学习了Android的开发.首先,必须要面 ...
- Asp.net MVC Request Life Cycle
Asp.net MVC Request Life Cycle While programming with Asp.net MVC, you should be aware of the life o ...
- CHANGE USER WHEN I CONNECT TO TEAM FOUNDATION SERVER
Question: I USE TEAM EXPLORER TO CONECT TO TEAM FOUNDATION SERVER 2010, BUT I DO NOT LOGIN OUT, AND ...
- java变量深入理解
4,变量:其实就是内存中的一个存储空间,用于存储常量数据. 作用:方便于运算.因为有些数据不确定.所以确定该数据的名词和存储空间. 特点:变量空间可以重复使用. 什么时候定义变量?只要是数据不确定的时 ...
- maven项目如何生成war文件
配置 你的pom.xml文件,在你的overview视窗里 配置 packaging为 war 然后然后点击 pom.xml右键,run as 选择 install 或是 package如果项目没问题 ...
- MyEclipse中的内置浏览器中的历史记录怎么清除
eclipse内置浏览器的访问记录是存储在对应的工程目录下的.metadata配置中,也就是说你新建一个工程的话就没有了. 如果确实要删除那就找到工作空间中的org.eclipse.ui.browse ...