[Backbone]3. More detail on View
Change the AppointmentView to have a top-level li tag (instead of the default div tag).
var AppointmentView = Backbone.View.extend({tagName: 'li'});
Make sure every AppointmentView top-level element is created with a class of appointment.
var AppointmentView = Backbone.View.extend({
tagName: 'li',
className: 'appointment'
});
Refactor the render function below to use the improved jQuery syntax on the top-level element.
var AppointmentView = Backbone.View.extend({
render: function(){
this.$el.html('<li>' + this.model.get('title') + '</li>');
}
});
Dr. Goodparts is getting ready to request some big changes to our AppointmentView. You know that eventually the HTML it generates is going to get pretty complicated, so now is probably a good time to refactor to use a template.
Make sure you generate the same HTML after switching to templates.
Tip: don't forget to use this.model.toJSON() in render
var AppointmentView = Backbone.View.extend({
render: function(){
this.$el.html('<span>' + this.model.get('title') + '</span>');
}
});
to
var AppointmentView = Backbone.View.extend({
template : _.template('<span><%= title %></span>'),
render: function(){
var attr = this.model.toJSON();
this.$el.html(this.template(attr));
}
});
Dr. Goodparts is just getting the hang of this web thing and thinks it'd be a good idea to alert the user the title of the appointment whenever they click on its view.
See if you can't appease his bad idea and implement this tragic UI interaction using View events.
var AppointmentView = Backbone.View.extend({
template: _.template('<span><%= title %></span>'),
events: {
"click": function(){alert(this.model.get('title'));}
},
render: function(){
this.$el.html(this.template(this.model.toJSON()));
}
});
Phew, over the weekend you convinced Dr. Goodparts to come to his senses and allow you to change the click event to only alert when the user double clicks on the view.
Make those changes quick and deploy!
var AppointmentView = Backbone.View.extend({
template: _.template('<span><%= title %></span>'),
events: { "dblclick span": "alertTitle" },
alertTitle: function(){
alert(this.model.get('title'));
},
render: function(){
this.$el.html(this.template(this.model.toJSON()));
}
});
-----------Final code------------
//Create a model CLASS
var Appointment = Backbone.Model.extend({});
//Define a object for model
var appointment = new Appointment({'title': "New appointment"});
//Create a View CLASS
/*var AppointmentView = Backbone.View.extend({
tagName: 'li',
className: 'appointment',
render: function(){
this.$el.html('<span>' + this.model.get('title') + '</span>');
}
});*/
//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><%= title %></span>'),
events: { "dblclick span": "alertTitle" },
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 });
//set title
appointment.set('title', "Nice to meet you!");
//Show the final view
appointmentView.render();
$('#app').html(appointmentView.el);
[Backbone]3. More detail on View的更多相关文章
- Backbone之旅——Collection and View篇
上篇文章说了Model,这次说说Collection,collection就是model的集合,用来装载model对象的 定义方法 var Persons = new Backbone.Collect ...
- [Backbone]2. More detail in Models
Our Appointment model doesn't seem too useful yet. Add two default attributes, title as the string & ...
- MVC、MVP、MVVM、Angular.js、Knockout.js、Backbone.js、React.js、Ember.js、Avalon.js、Vue.js 概念摘录
注:文章内容都是摘录性文字,自己阅读的一些笔记,方便日后查看. MVC MVC(Model-View-Controller),M 是指业务模型,V 是指用户界面,C 则是控制器,使用 MVC 的目的是 ...
- RequireJS与Backbone简单整合
前言 昨天我们一起学习了Backbone,最后做了一个备忘录的例子,说是做了不如说是看了下官方提供的例子,所以最终我感觉我们还是没能掌握Backbone,今天还得做个其它例子先. 然后前面也只是草草学 ...
- Backbone框架浅析
Backbone是前端mvc开发模式的框架.它能够让view和model相分离,让代码结构更清晰简答,开发进度加快,维护代码方便.但是,现在出了一种mvvm框架,它是下一代前端mvc开发模式的框架,代 ...
- 最轻量级的前端Mvc框架backbone
最轻量级的前端Mvc框架backbone依赖最轻量级的库understore backbone并非将前端再次切分为mvc,而是分为了七大模块,分别是:Events.Model.Collection.R ...
- React和Backbone优缺点
1.React 使用了VDOM,方便移植至其他平台,如Android等:Backbone更灵活,且与Jquery结合比较好. 2.React如果不与Jsx结合易读性很差;Backbone有强大的模板功 ...
- [转]Angular, Backbone, or Ember: Which is Best for your Build?
In order to choose which framework is right for your build, we've asked four important questions of ...
- Table View Programming Guide for iOS---(六)---A Closer Look at Table View Cells
A Closer Look at Table View Cells A table view uses cell objects to draw its visible rows and then c ...
随机推荐
- bzoj 3252: 攻略
3252: 攻略 Description 题目简述:树版[k取方格数] 众所周知,桂木桂马是攻略之神,开启攻略之神模式后,他可以同时攻略k部游戏. 今天他得到了一款新游戏<XX半岛>, ...
- HTTP首部字段
HTTP首部由首部字段名和首部字段值组成,以逗号隔开.如果首部出现重复,有些浏览器优先处理第一个出现的首部,有些优先处理后者. 主要分为四大类 通用首部字段 请求首部字段 响应首部字段 实体首部字段 ...
- bzoj 2406 二分+有源有汇上下界网络流可行流判定
弱爆了,典型的行列建模方式,居然想不到,题做少了,总结少了...... 二分答案mid s----------------------->i行-----------------------> ...
- bzoj 1875: [SDOI2009]HH去散步 -- 矩阵乘法
1875: [SDOI2009]HH去散步 Time Limit: 20 Sec Memory Limit: 64 MB Description HH有个一成不变的习惯,喜欢饭后百步走.所谓百步走, ...
- Codeforces Round #298 (Div. 2) E. Berland Local Positioning System 构造
E. Berland Local Positioning System Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.c ...
- Android设备运用Clockworkmod Recovery恢复模式安装定制的Rom
Clockworkmod Recovery是一个由Cyanogen团队开发的用于Android设备的第三方定制Recovery恢复模式,也称为CWM Recovery,具体它有什么用处呢?请看关于Go ...
- hint.css使用说明
GitHub:http://liu12fei08fei.github.io/html/1hint.html hint.css使用说明 用途 快速实现tooltips提示样式 相关资源 官方网站GitH ...
- linux普通用户获取管理员权限
原文:http://www.cnblogs.com/likwo/p/3435404.html 测试环境:CentOS 5.5 1.添加用户,首先用adduser命令添加一个普通用户,命令如下: #ad ...
- POJ 3740 Dancing Links
Dancing Links学习:http://www.cnblogs.com/steady/archive/2011/03/15/1984791.html 以及图文学习:http://www.cnbl ...
- python接口自动化6-重定向(Location)
前言 某屌丝男A鼓起勇气向女神B打电话表白,女神B是个心机婊觉得屌丝男A是好人,不想直接拒绝于是设置呼叫转移给闺蜜C了,最终屌丝男A和女神闺蜜C表白成功了,这种场景其实就是重定向了. 一.重定向 1. ...