[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 ...
随机推荐
- JMS开发指南
1.JMS消息的异步与同步接收 消息的异步接收: 异步接收是指当消息到达时,主动通知客户端,即当消息到达时转发到客户端.JMS客户端可以通过注册一个实现MessageListener接口的对象到Mes ...
- luoguP4360 [CEOI2004]锯木厂选址
题目链接 luoguP4360 [CEOI2004]锯木厂选址 题解 dis:后缀和 sum:前缀和 补集转化,减去少走的,得到转移方程 dp[i] = min(tot - sumj * disj - ...
- UOJ275 组合数问题
给定n,m和k,求有多少对(i , j)满足0 ≤ i ≤ n, 0 ≤ j ≤ min(i ,m)且C(︀i,j)︀是k的倍数.n,m ≤ 1018, k ≤ 100,且k是质数. 把i和j都看成k ...
- [SPOJ-BEADS]Glass Beads
来源: CE1998 题目大意: 求字符串最小表示. 思路: 字符串复制一遍接在后面,构建SAM,然后每次跑小的转移. 跑n次以后就跑到了最小表示的末尾,用该状态的len值减去n就是最小表示的起始位置 ...
- php <a href></a>链接地址中是php变量,链接文本也是php变量的代码处理方法
1.所用php变量名为$recent_tests,是一个二维数组,示例如下: $recent_tests[0]["test_url"] = www.baidu.com $recen ...
- 读书笔记_Effective_C++_条款三十一:将文件间的编译依存关系降至最低(第三部分)
下面来谈谈书中的第二部分,用Interface Classes来降低编译的依赖.从上面也可以看出,避免重编的诀窍就是保持头文件(接口)不变化,而保持接口不变化的诀窍就是不在里面声明编译器需要知道大小的 ...
- Git_多人协作
当你从远程仓库克隆时,实际上Git自动把本地的master分支和远程的master分支对应起来了,并且,远程仓库的默认名称是origin. 要查看远程库的信息,用git remote: $ git r ...
- PHP通过AJAX及Access-Control-Allow-Origin实现跨域访问
这里的跨域实质上是由浏览器同源策略限制的一类请求场景,浏览器同源策略SOP(Same origin policy)是一种约定,由Netscape公司1995年引入浏览器,它是浏览器最核心也最基本的安全 ...
- POJ 3468 A Simple Problem with Integers (splay tree入门)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 47944 ...
- 关于Google Android平台的ClockworkMod Recovery恢复模式
lockworkMod Recovery,它也被称为Clockwork与CWM,它是装载Google Android操作系统设备的一个自定义的Recovery恢复模式,它可以使得相关Android设备 ...