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 cancelfunction 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的更多相关文章

  1. [Backbone]4. Model & View, toggle between Model and View. -- 1

    如上图所示: Server有Data都交给Models处理, 然后由Models给Views Data,让View去告诉DOM如何显示, 然后DOM显示HTML; View events update ...

  2. 第15.18节 PyQt(Python+Qt)入门学习:Model/View架构中视图Item Views父类详解

    老猿Python博文目录 老猿Python博客地址 一.概述 在PyQt图形界面中,支持采用Model/View架构实现数据和界面逻辑分离,其中Model用于处理数据存储,View用于界面数据展现,当 ...

  3. (转)Qt Model/View 学习笔记 (六)——在views中选择数据项

    在views中选择数据项 概念 用于新的view类中的选择模型比Qt3中的模型有了很大的改进.它为基于model/view架构的选择提供了更为全面的描述.尽管对提供了的views来说,负责操纵选择的标 ...

  4. 第十五章、Model/View架构中Item Views部件的父类

    老猿Python博文目录 老猿Python博客地址 引言:本章早就写好了,其简版<第15.18节 PyQt(Python+Qt)入门学习:Model/View架构中视图Item Views父类详 ...

  5. (转)Qt Model/View 学习笔记 (四)——创建新的Models

    创建新的Models 介绍 model/view组件之间功能的分离,允许创建model利用现成的views.这也可以使用标准的功能 图形用户接口组件像QListView,QTableView和QTre ...

  6. Qt Model/View(官方翻译,图文并茂)

    http://doc.trolltech.com/main-snapshot/model-view-programming.html 介绍 Qt 4推出了一组新的item view类,它们使用mode ...

  7. (转)Qt Model/View 学习笔记 (七)——Delegate类

    Qt Model/View 学习笔记 (七) Delegate  类 概念 与MVC模式不同,model/view结构没有用于与用户交互的完全独立的组件.一般来讲, view负责把数据展示 给用户,也 ...

  8. (转)Qt Model/View 学习笔记 (二)——Qt Model/View模式举例

    Qt Model/View模式举例 Qt提供了两个标准的models:QStandardItemModel和QDirModel.QStandardItemModel是一个多用途的model,可用于表示 ...

  9. (转)Qt Model/View 学习笔记 (一)——Qt Model/View模式简介

    Qt Model/View模式简介 Qt 4推出了一组新的item view类,它们使用model/view结构来管理数据与表示层的关系.这种结构带来的 功能上的分离给了开发人员更大的弹性来定制数据项 ...

随机推荐

  1. BZOJ 2302: [HAOI2011]Problem c [DP 组合计数]

    2302: [HAOI2011]Problem c Time Limit: 30 Sec  Memory Limit: 256 MBSubmit: 648  Solved: 355[Submit][S ...

  2. BZOJ3207 花神的嘲讽计划

    hash值建主席树. 垃圾题面没有熟虑范围害我MLE——>RE. By:大奕哥 #include<bits/stdc++.h> #define unll unsigned long ...

  3. Windows Server 2008 R2下将nginx安装成windows系统服务

    一直在Linux平台上部署web服务,但是最近的一个项目,必须要用windows,不得已再次研究了nginx在windows下的表现,因为Apache httpd在Windows下表现其实也不算太好, ...

  4. bzoj 2244

    没有正确分析路径可能的条数,它是指数增长的,会爆long long. 然后就是正反两次时间分治. 另一个就是max with count,即带计数的最值,即除了记录最值,还要记录最值取得的次数. /* ...

  5. BZOJ 4726 POI 2017 Sabota? 树形DP

    4726: [POI2017]Sabota? Time Limit: 20 Sec  Memory Limit: 128 MBSec  Special JudgeSubmit: 128  Solved ...

  6. PYQT设计无边框窗体

    #UI.py,通过UI设计师制作后直接转换为UI.py脚本 # -*- coding: utf-8 -*-from PyQt4 import QtCore, QtGui try:    _fromUt ...

  7. Codeforces Round #228 (Div. 1) A. Fox and Box Accumulation 贪心

    A. Fox and Box Accumulation 题目连接: http://codeforces.com/contest/388/problem/A Description Fox Ciel h ...

  8. 浏览器数据库IndexedDB介绍

    摘要 在移动端H5页面开发的时候,为了更好的提高用户体验,可以对不常变化的数据做浏览器端数据缓存,在用户打开页面的时候,首先加载本地的数据,然后异步请求服务端,更新数据.在移动端webview中,可以 ...

  9. jQuery碎语(1) 基础、选择要操作的元素、处理DOM元素

    1.基础 jquery对象集: $():jquery对象集合 获取jquery对象集中的元素: 使用索引获取包装器中的javascript元素:var temp = $('img[alt]')[0] ...

  10. 05引用类型以及特殊引用类型string

    基本 □ 哪些属于引用类型 类(object,string),接口.数组.委托 □ 引用类型分配在哪里 ● 引用类型变量位于线程栈. ● 引用类型实例分配在托管堆上. ● 当引用类型实例的大小小于85 ...