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. 阿里云腾讯云服务器ubuntu多域名配置

    1.将域名A记录解析到服务器的公网 IP 地址,把www也添加A记录到公网ip 2.ubuntu系统修改hosts文件,hosts文件目录为/etc/hosts,可以用vim编辑  sudo vim ...

  2. JMS异步消息机制

    企业消息系统 Java Message Service 是由 Sun Microsystems 开发的,它为 Java 程序提供一种访问 企业消息系统 的方法.在讨论 JMS 之前,我们分来析一下企业 ...

  3. Codeforces Round #502 (in memory of Leopoldo Taravilse, Div. 1 + Div. 2) G. The Tree

    G. The Tree time limit per test 3 seconds memory limit per test 256 megabytes input standard input o ...

  4. hdu 1698 线段树 成段更新

    题意:一段钩子,每个钩子的值为1,有若干更新,每次跟新某段的值,若干查询某段的和 基础题了 #include<cstdio> #include<iostream> #inclu ...

  5. wikioi 1017 乘积最大

    dp[i][j]=max(dp[i][j],dp[t][k-1]*mapn[t+1][i]); dp[i][j]代表从0-i之间有j个乘号,mapn[i][j]表示第i位到第j位的数究竟是多少 #in ...

  6. Python—对Excel进行读写操作

    学习Python的过程中,我们会遇到Excel的读写问题.通过搜索得知,我们可以使用xlwt module将数据写入Excel表格,使用xlrd module从Excel读取数据.下面介绍如何实现使用 ...

  7. MySQL内核整理(一)

    一.在共享表空间(系统表空间)中,innodb会维护一些系统信息:1.Internal data dictionary2.Rollback segments3.undo space4.insert b ...

  8. js解析顺序了解一下??

    我们在学习一种新事物的时候,总是知其然,而不知其所然.有些人会探究到底,有一些人会得过且过. 好了,开场白结束,直接进入正题. js不像C语言那样只要编译一次之后成.exe文件之后就不用在编译可以直接 ...

  9. intellij idea 部署项目的时候 图中application context 写不写有什么关系?有什么作用?

    这个就是你部署之后访问的路径,比如你写一个/test,那反问就是127.0.0.1:8080/test,没有写的话就是127.0.0.1:8080

  10. GIT 详解2

    https://segmentfault.com/a/1190000000738398 http://www.cnblogs.com/cposture/p/4903767.html https://g ...