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的更多相关文章

  1. Backbone之旅——Collection and View篇

    上篇文章说了Model,这次说说Collection,collection就是model的集合,用来装载model对象的 定义方法 var Persons = new Backbone.Collect ...

  2. [Backbone]2. More detail in Models

    Our Appointment model doesn't seem too useful yet. Add two default attributes, title as the string & ...

  3. 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 的目的是 ...

  4. RequireJS与Backbone简单整合

    前言 昨天我们一起学习了Backbone,最后做了一个备忘录的例子,说是做了不如说是看了下官方提供的例子,所以最终我感觉我们还是没能掌握Backbone,今天还得做个其它例子先. 然后前面也只是草草学 ...

  5. Backbone框架浅析

    Backbone是前端mvc开发模式的框架.它能够让view和model相分离,让代码结构更清晰简答,开发进度加快,维护代码方便.但是,现在出了一种mvvm框架,它是下一代前端mvc开发模式的框架,代 ...

  6. 最轻量级的前端Mvc框架backbone

    最轻量级的前端Mvc框架backbone依赖最轻量级的库understore backbone并非将前端再次切分为mvc,而是分为了七大模块,分别是:Events.Model.Collection.R ...

  7. React和Backbone优缺点

    1.React 使用了VDOM,方便移植至其他平台,如Android等:Backbone更灵活,且与Jquery结合比较好. 2.React如果不与Jsx结合易读性很差;Backbone有强大的模板功 ...

  8. [转]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 ...

  9. 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 ...

随机推荐

  1. 数据准备<4>:变量筛选-理论篇

    在上一篇文章<数据准备<3>:数据预处理>中,我们提到降维主要包括两种方式:基于特征选择的降维和基于维度转换的降维,其中基于特征选择的降维通俗的讲就是特征筛选或者变量筛选,是指 ...

  2. weighttp 使用

    Weighttp 地址 http://redmine.lighttpd.net/projects/weighttp/wiki Weighttp的介绍:weighttp  is a lightweigh ...

  3. Python168的学习笔记5

    关于对csv文件的操作. python标准库中有csv的库,使用非常方便. import csv with open('pingan.csv','rb') as rf: reader = csv.re ...

  4. hdoj 5120 Intersection 圆环面积求交

    Intersection Time Limit: 4000/4000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others) Tota ...

  5. Codeforces Round #294 (Div. 2)B - A and B and Compilation Errors 水题

    B. A and B and Compilation Errors time limit per test 2 seconds memory limit per test 256 megabytes ...

  6. Android 性能监控系列一(原理篇)

    欢迎关注微信公众号:BaronTalk,获取更多精彩好文! 一. 前言 性能问题是导致 App 用户流失的罪魁祸首之一,如果用户在使用我们 App 的时候遇到诸如页面卡顿.响应速度慢.发热严重.流量电 ...

  7. oracle开发学习篇之集合运算符以及集合异常捕获

    --取出集合;长度 declare type list_nested ) not null; v_all list_nested := list_nested('a','b','c','d','c', ...

  8. delphi 如何判断应用程序未响应

    http://www.cnblogs.com/smallmuda/archive/2009/07/24/1529845.html delphi 如何判断应用程序未响应    今天在MSN的核心讨论组上 ...

  9. MVC文件上传07-使用客户端jQuery-File-Upload插件和服务端Backload组件裁剪上传图片

    本篇通过在配置文件中设置,对上传图片修剪后保存到指定文件夹. 相关兄弟篇: MVC文件上传01-使用jquery异步上传并客户端验证类型和大小  MVC文件上传02-使用HttpPostedFileB ...

  10. INDY10 IDHTTPSERVER返回中文不乱码

    INDY10 IDHTTPSERVER返回中文不乱码 procedure TynHttpServer.CommandGet(AContext: TIdContext; ARequestInfo: TI ...