[Backbone] Verying Views
Below we have our AppointmentsView instance rendering and then taking the rendered HTML and inserting into the$('#app') element.
Change the code to instead pass in the $('#app') element into the view constructor to make it theappointmentsView.el.
var appointmentsView = new AppointmentsView({collection: appointments, el: $('#app')});
appointmentsView.render();
Update the AppointmentsView class to handle the extra option doctor passed into the constructor, like so: new AppointmentsView({collection: appointments, doctor: drGoodparts}) Assign the extra option to thedoctor property on the view instance.
var AppointmentsView = Backbone.View.extend({
initialize: function(options){
this.doctor = options.doctor;
}
});
Dr. Goodparts recently hired an intern to input appointments and they've been injecting appointments with malicious titles to hack Dr. Goodparts' computer.
The intern was fired but you should probably update the AppointmentView to escape the title content.
var AppointmentView = Backbone.View.extend({
template: _.template("<span><%= model.escape('title') %></span>"),
render: function(){
this.$el.html(this.template({model: this.model}));
}
});
As you can see in the view code below, whenever the model's title attribute changes, we update the title in the view and highlight it to let the user know that it's been updated. Sometimes we want to be able to change the title without highlighting the view, but with still updating it in the view.
To accomplish this, we are passing in {highlight: false}. Update the changedTitle function below to use this extra option to selectively highlight the view.
var AppointmentView = Backbone.View.extend({
template: _.template("<span><%= title %></span>"),
initialize: function(){
this.model.on('change:title', this.changedTitle, this);
},
render: function(){
this.$el.html(this.template(this.model.attributes));
},
changedTitle: function(model, value, option){
this.$('span').html(value);
if(option.highlight!=false){
this.$el.effect('highlight', {}, 1000);
}
}
});
Use the new listenTo View function to make the view listen to the model's 'change:title' event, instead of having the model notify the view of the event. This way we can safely call remove() on the view and feel confident all of our events are cleaned up.
var AppointmentView = Backbone.View.extend({
template: _.template("<span><%= title %></span>"),
initialize: function(){
this.listenTo(this.model, 'change:title', this.render);
},
render: function(){
this.$el.html(this.template(this.model.attributes));
},
changedTitle: function(model, value, options){
this.$('span').html(value);
if (options.highlight !== false){
this.$el.effect('highlight', {}, 1000);
}
}
});
[Backbone] Verying Views的更多相关文章
- TodoMVC中的Backbone+MarionetteJS+RequireJS例子源码分析之三 Views
这个版本的TodoMVC中的视图组织划分比较细,更加易于理解,这也得益于Marionette为我们带来了丰富的视图选择,原生的backbone只有views,而Marionette则有itemview ...
- Backbone.js 为复杂Javascript应用程序提供模型(models)、集合(collections)、视图(views)的结构
Backbone.js 为复杂Javascript应用程序提供模型(models).集合(collections).视图(views)的结构.其中模型用于绑定键值数据和 自定义事件:集合附有可枚举函数 ...
- [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 a ...
- [Backbone]7. Collection Views, Custom Events
It's finally time to start building out our Appointment app. We're going to be using a collection an ...
- backbone入门示例
最近因为有个项目需要用backbone+mui 所以最近入坑backbone. Backbonejs有几个重要的概念,先介绍一下:Model,Collection,View,Router.其中Mod ...
- 前端MVC框架Backbone 1.1.0源码分析(一)
前言 如何定义库与框架 前端的辅助工具太多太多了,那么我们是如何定义库与框架? jQuery是目前用的最广的库了,但是整体来讲jQuery目的性很也明确针对“DOM操作”,当然自己写一个原生态方法也能 ...
- 前端MVC框架Backbone 1.1.0源码分析(二) - 模型
模型是什么? Models are the heart of any JavaScript application, containing the interactive data as well a ...
- 用backbone实现的一个MVC的小demo
一.Apache配置 本实例需要使用php支持.要现在Apache中配置虚拟目录,在Apache下的httpd-vhosts.conf文件中添加如下代码 <VirtualHost *:80> ...
- 实践:Backbone作前端,Django+Tastypie作后端的简单Web在线聊天室
一.界面设计: 二.数据模型设计 id 每个发言都有一个独立的id由tastypie自动生成 content 发言的内容 username 发言者 date 发言时间 三.前端制作 这里没有用到Bac ...
随机推荐
- Codeforces Round #222 (Div. 1) A. Maze dfs
A. Maze 题目连接: http://codeforces.com/contest/377/problem/A Description Pavel loves grid mazes. A grid ...
- hdu 4451 Dressing 排列组合/水题
Dressing Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- SMACH(五)----用户数据UserData类和重映射Remapper类的原理和例子
用户数据UserData类和重映射Remapper类包含在smach中的user_data.py文件中实现,该博文主要介绍其原理和例子 UserData主要用于状态之间的数据传递,包括数据的输入inp ...
- 在C#中实现视频播放器
当我们需要在C#中实现视频播放器的时候,可以使用如下几种方法: 一.使用MediaPlayer ActiveX控件 在C#中支持视屏播放器最简单的方式就是插入MediaPlayer控件了,在WPF中还 ...
- 事件冒泡与捕获&事件托付
设想这样一种情况 一个div里面有个span元素 ,当鼠标单击span时,这个事件算是谁的? div还是span? 准确的说两个都触发了,这种认可大家都允许,事实就是这种, 第二个问题来了,这个事件 ...
- easyui中combobox 验证输入的值必须为选项框中的数据
当作为提示框的方式时,combobox必须设置为允许用户输入的模式,但是当用户输入后未选择正确的数据就直接按tab或点击鼠标离开控件会导致用户输入无效的值并且通过验证,为了避免这种情况的发生我们需要对 ...
- linux 内核大牛-谢宝友
http://blog.chinaunix.net/uid/25845340.html 谢宝友:毕业于四川省税务学校税收专业,现供职于中兴通讯操作系统团队,对操作系统内核有较强的兴趣.专职于操作系统内 ...
- Save a 32-bit Bitmap as 1-bit .bmp file in C#
What is the easiest way to convert and save a 32-bit Bitmap to a 1-bit (black/white) .bmp file in C# ...
- 【docker】docker启动、重启、关闭命令,附带:docker启动容器报错:docker: Error response from daemon: driver failed programming external connectivity on endpoint es2-node
在关闭并放置centos 的防火墙重启之后[操作:https://www.cnblogs.com/sxdcgaq8080/p/10032829.html] 启动docker容器就发现开始报错: [ro ...
- PHP 7.0 5.6 下安裝 phpLDAPadmin 发生错误的修正方法
在稍具規模的網路環境中, 網管時常選用 LDAP 來進行帳號的統整管理, 一方面提供管理便利度, 另一方面使用者也不必因為不同系統而記憶不同帳號, phpLDAPadmin 是一套常見的 LDAP 管 ...