[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 ...
随机推荐
- Sublime Text2 默认语言(windows/unix)设置,Sublime插件大全
Sublime默认系统语言设置 Sublime Text 2默认使用的就是UTF8,这个UTF8模式使用的是不带BOM的,如果要修改这个配置,到Perference->Settings-User ...
- git服务端和客户端百度网盘下载地址
https://pan.baidu.com/s/1BKw-bgYOrQjLkwUMzyH7KQ
- js解析顺序了解一下??
我们在学习一种新事物的时候,总是知其然,而不知其所然.有些人会探究到底,有一些人会得过且过. 好了,开场白结束,直接进入正题. js不像C语言那样只要编译一次之后成.exe文件之后就不用在编译可以直接 ...
- Spring @Value 用法小结,#与$的区别
20161016更新:这货其实是SpEL的功能,来这里看看吧: Spring 4 官方文档学习(五)核心技术之SpEL 起因 一直的用法是 @Value("${jdbc.driverClas ...
- FolderSync Instant sync 即时同步
Folderpairs - Edit folderpair - Sync options - Instant sync Select this for instant sync on change. ...
- linux strace追踪mysql执行语句 (mysqld --debug)
转载请注明出处:使用strace追踪多个进程 http://www.ttlsa.com/html/1841.html http://blog.itpub.net/26250550/viewspace- ...
- How to check Ubuntu version
Below you can find some tips on how to check Ubuntu version you are currently running. The first pla ...
- VisualSVN Server 修改用户密码
VisualSVN Server是非常方便好用的SVN服务器端软件,但有个问题,你在服务器端创建了用户名密码后,用户无法自己修改密码,据说VisualSVN的客户端可以修改用户密码,但客户端是收费软件 ...
- python笔记29-队列Queue
前言 Python的Queue模块提供一种适用于多线程编程的FIFO实现.它可用于在生产者(producer)和消费者(consumer)之间线程安全(thread-safe)地传递消息或其它数据,因 ...
- cocos2d-x动画加速与减速
动画是游戏的必定要素之中的一个,在整个游戏过程中,又有着加速.减速动画的需求.以塔防为样例,布塔的时候希望可以将游戏减速,布好塔后,则希望能将游戏加速:当某个怪被冰冻后,移动速度减缓,而其它怪的移动速 ...