[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 ...
随机推荐
- POJ 3177 Redundant Paths 双联通分量 割边
http://poj.org/problem?id=3177 这个妹妹我大概也曾见过的~~~我似乎还没写过双联通分量的blog,真是智障. 最少需要添多少条边才能使这个图没有割边. 边双缩点后图变成一 ...
- java后台接收json数据,报错com.alibaba.fastjson.JSONObject cannot be cast to xxx
从前台接收json封装的list数据,在后台接收时一直报错,com.alibaba.fastjson.JSONObject cannot be cast to xxx, 使用这种方式接收可以接收 @R ...
- jquery中的 $.fn $.fx
$.fn是指 jquery的命名空间,加上fn上的方法及属性,会对jquery实例每一个有效. 如扩展$.fn.abc() 那么你可以这样子:$("#div").abc(); 常使 ...
- .vs目录有什么用?
写这篇博文的目的就是方便后来者能够在百度里轻松搜到. 反正我找了半天没找到关于.vs目录的介绍,最后还是在同事的帮助下才找到的. 参考地址:https://developercommunity.vis ...
- cuda并行编程之求解ConjugateGradient(共轭梯度迭代)丢失dll解决方式
在进行图像处理过程中,我们常常会用到梯度迭代求解大型线性方程组.今天在用cuda对神秘矩阵进行求解的时候.出现了缺少dll的情况: 报错例如以下图: watermark/2/text/aHR0cDov ...
- js:深入prototype(上:内存分析)
/** * 下面演示了通过原型的创建方式,使用基于原型的创建能够将属性和方法 * 设置为Person专有的,不能通过window来调用. * 原型是javascript中的一个特殊对象,当一个函 ...
- 成为Java GC专家
http://www.importnew.com/author/wangxiaojie
- docker 图解学习
http://www.cnblogs.com/bethal/tag/docker/
- MySQL性能诊断与调优 转
http://www.cnblogs.com/preftest/ http://www.highperfmysql.com/ BOOK LAMP 系统性能调优,第 3 部分: MySQL 服务 ...
- .Net高级技术——垃圾收集器
垃圾收集器概述 大排档和学校食堂.一个是别人帮你收拾盘子,一个是自己收拾盘子. 垃圾收集GC(Garbage Collection).内存的分配.回收不需要程序员操心,程序员只要需要的时候new就可以 ...