Welcome to the Anatomy of Backbone.js challenges! We're going to be building a simple Appointment app for our friend, Dr. Goodparts.

So, let's get started and create an Appointment model class.

var Appointment = Backbone.Model.extend({});

Now that we have our Appointment model class, let's create our first instance and assign it to the appointmentvariable. Pass in a title for the appointment when creating it.

Note Check out the JS Source tab below that will display relevant code for this challenge.

//Define a object for model
var appointment = new Appointment({'title': "New appointment"});

Well look at that, we have our very first appointment. But it isn't so useful, sitting there deep down in the bowels of your browser. To display it lets first create a view class and name it AppointmentView.

//Create a View CLASS
var AppointmentView = Backbone.View.extend({});

The perfect place to put our display code now exists! Now it's time to create our first AppointmentView instance. When doing so, don't forget to include the appointment model instance we just created. Assign the instance to a variable.

var appointmentView = new AppointmentView({model: appointment });

Our AppointmentView instance is almost ready, all we have to do is go back and define the AppointmentViewrender function so we can actually create some HTML. Have the render function add an <li> tag to the top-level element of the view. Use this.model.get('title') as the content of the <li>.

var AppointmentView = Backbone.View.extend({
render: function(){
var html = '<li>'+this.model.get('title')+'</li>';
$(this.el).html(html);
}
});

Almost there, but before we display anything let's set the title of the appointment instance! Set it to any string, perhaps something you'd like Dr. Goodparts to take a look at (ex. My Backbone Hurts).

appointment.set('title', "Nice to meet you!");

Time to show Dr. Goodparts his first appointment. Render the appointmentView instance and then insert its top-level element into #app using $('#app').html().

//Show the final view
appointmentView.render();
$('#app').html(appointmentView.el);

-----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({
render: function(){
var html = '<li>'+this.model.get('title')+'</li>';
$(this.el).html(html);
}
});
//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]1. Module, View classed的更多相关文章

  1. backbone库学习-View

    Backbone中的视图提供了一组处理DOM事件.和渲染模型(或集合)数据方法(在使用视图之前,你必须先导入jQuery或Zepto) 视图类提供的方法非常简单,我们一般在backbone.View的 ...

  2. Backbone中父子view之间的值传递

    backbone中,使用最多的莫过于在view中进行操作,如模板的渲染以及事件函数的定义.为了提高代码的可维护性,一般地我们会写多个视图即view,将界面按照功能的不同进行模块化划分,模块与view一 ...

  3. Backbone.js 的 View 中定义事件

    使用 Backbone 的 View 时,可以象传统 jQuery 那样定义事件,$("selector").click(function(){...}).幸运的是 Backbon ...

  4. Backbone学习笔记 - View篇

    Backbone是一种Web端的MVC框架,这里纪录学习Model,View和Collection的笔记. 1 View initialize构造函数 Backbone.View 与jQuery库紧密 ...

  5. [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 ...

  6. backbone之module

    上一篇列出了collection的代码,下面要把代码进行分离 //先是app.js var ContactManager = new Marionette.Application(); Contact ...

  7. [Backbone]4. Model & View, toggle between Model and View. -- 1

    如上图所示: Server有Data都交给Models处理, 然后由Models给Views Data,让View去告诉DOM如何显示, 然后DOM显示HTML; View events update ...

  8. Backbone中 View之间传值的解决办法

    Backbone中的View就是用来展示由Model层传出的数据,或者在View里产生的一些数据,包括输入框中输入等产生的数据,由当前View传递到另外一个View层里,应该怎么办呢,我之前读到一位博 ...

  9. 简说一下coffeescript的constructor是如何导致Backbone.View的事件无法正常工作的.

    在继承方面,js还是弱项呀.发现在继承的时候constructor和initialize之分.网上文章没有说明二者关系.看了源码才发现二者的区别呀. 首先我用coffeescript来实现js的继承, ...

随机推荐

  1. 【20181031T2】几串字符【数位DP思想+组合数】

    题面 [错解] 一眼数位DP 设\(f(i,c00,c01,c10,c11)\)-- 神tm DP 哎好像每两位就一定对应c中的一个,那不用记完 所以可以设\(f(i,c00,c01,c10)\)-- ...

  2. Codeforces Round #493 (Div 2) (A~E)

    目录 Codeforces 998 A.Balloons B.Cutting C.Convert to Ones D.Roman Digits E.Sky Full of Stars(容斥 计数) C ...

  3. BZOJ 4726 POI 2017 Sabota? 树形DP

    4726: [POI2017]Sabota? Time Limit: 20 Sec  Memory Limit: 128 MBSec  Special JudgeSubmit: 128  Solved ...

  4. 一行代码提取url中querystring的某个key的值

    var itemdata = "OrderFilter=0&ProjectTag=15&DateType=0"; var projectTag = itemdata ...

  5. USB Mass Storage大容量存储 The Thirteen Class章节的理解

    http://blog.csdn.net/xgbing/article/details/7002558 USB Mass Storage 6.7 The Thirteen Class章节的理解 Cas ...

  6. Java基础加强总结(二)——泛型

    一.体验泛型 JDK1.5之前的集合类中存在的问题——可以往集合中加入任意类型的对象,例如下面代码: package cn.gacl.generic.summary; import java.util ...

  7. 03把IL编译成可执行文件

    1.在记事本中编写IL代码如下: .assembly HelloWorld{} .assembly extern mscorlib{}   .method public static void Mai ...

  8. jQuery判断复选框是否被选中的3种方式

    页面部分:     <input type="checkbox" id="cbx" /><label for="cbx"& ...

  9. Ransac 与 最小二乘(LS, Least Squares)拟合直线的效果比较

    代码下载地址      http://pan.baidu.com/s/1eQIzj3c 进入目录后,请自行定位到该博客的源代码与数据的目录“

  10. SQL游标、函数的使用方法

         游标的的使用有日常的开发和维护的过程不使用的并不多,但是碰到一些棘手的问题的时候,游标时常是个非常好的帮手,下面就说下游标的使用方法,方法自己以后查阅,和加深一些印象,下面以一个存储过程为例 ...