[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结构来管理数据与表示层的关系.这种结构带来的 功能上的分离给了开发人员更大的弹性来定制数据项 ...
随机推荐
- linux服务器安装swoole扩展
说明: swoole只能用在LInux,macos系统上,不能用作Windows系统上 2.0.12版本开始不再支持PHP5 安装方式一:pecl安装 适用于php7.0以上版本 centOS中: # ...
- 数据准备<5>:变量筛选-实战篇
在上一篇文章<数据准备<4>:变量筛选-理论篇>中,我们介绍了变量筛选的三种方法:基于经验的方法.基于统计的方法和基于机器学习的方法,本文将介绍后两种方法在Python(skl ...
- [BZOJ4811][YNOI2017]由乃的OJ(树链剖分+线段树)
起床困难综合症那题,只要从高往低贪心,每次暴力跑一边看这一位输入0和1分别得到什么结果即可. 放到序列上且带修改,只要对每位维护一个线段树,每个节点分别记录0和1从左往右和从右往左走完这段区间后变成的 ...
- leetcode659. Split Array into Consecutive Subsequences
leetcode659. Split Array into Consecutive Subsequences 题意: 您将获得按升序排列的整数数组(可能包含重复项),您需要将它们拆分成多个子序列,其中 ...
- js面向对象写页面
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- lanmp安装一(centos+apache+nginx+mysql+php=lanmp地址下载)
背景 centos7 官网地址https://www.centos.org/download/ 下载选择DVD版进入(也就是标准版,系统齐全) 点击任何一个国家的下载链接 下载地址 http://m ...
- cloudstack openstack zstack
http://www.cnblogs.com/skyme/archive/2013/06/06/3118852.html http://www.niubua.com/ http://zstack.or ...
- 光速 React
光速 React Vixlet 团队优化性能的经验教训 在过去一年多,我们 Vixlet 的 web 团队已经着手于一个激动人心的项目:将我们的整个 web 应用迁移到 React + Redux 架 ...
- java基础学习总结——对象转型
一.对象转型介绍 对象转型分为两种:一种叫向上转型(父类对象的引用或者叫基类对象的引用指向子类对象,这就是向上转型),另一种叫向下转型.转型的意思是:如把float类型转成int类型,把double类 ...
- ios的一些知识点
ios的一些知识点 一 非ARC的内存管理情况 1-autorelease,当用户的代码在持续运行时,自动释放池是不会被销毁的,这段时间内用户可以安全地使用自动释放的对象.当用户的代码运行告一段落,开 ...