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. zookeeper【3】服务发现

    服务发现:指对集群中的服务上下线做统一管理,每个工作服务器都可以作为数据的发布方,向集群注册自己的基本信息,而让某些监控服务器作为订阅方,订阅工作服务器的基本信息.当工作服务器的基本信息改变时,如服务 ...

  2. 【洛谷】NOIP提高组模拟赛Day1【组合数学】【贪心+背包】【网络流判断是否满流以及流量方案】

    U41568 Agent1 题目背景 2018年11月17日,中国香港将会迎来一场XM大战,是世界各地的ENLIGHTENED与RESISTANCE开战的地点,某地 的ENLIGHTENED总部也想派 ...

  3. 几个常用的Eclipse插件

    用Eclipse Neon做ROS开发需要几个常用的插件,可以大大加速开发的进度. 1.常用插件 a.CMake Editer 地址:http://cmakeed.sourceforge.net/ec ...

  4. 极路由通过SSH添加静态路由表之后无法跳转的问题

    1.确定系统已经开启了转发功能: /etc/sysctl.conf下的配置项目为net.ipv4.ip_forward = 1 2.关闭防火墙的REJECT,也就是修改/etc/config/fire ...

  5. HDU 4649 Professor Tian(反状态压缩dp,概率)

    本文出自   http://blog.csdn.net/shuangde800 题目链接:点击打开链接 题目大意 初始有一个数字A0, 然后给出A1,A2..An共n个数字,这n个数字每个数字分别有一 ...

  6. 分频器VHDL描述

    在数字电路中,常需要对较高频率的时钟进行分频操作,得到较低频率的时钟信号.我们知道,在硬件电路设计中时钟信号时非常重要的.    下面我们介绍分频器的VHDL描述,在源代码中完成对时钟信号CLK的2分 ...

  7. Android 脚本设计之 SL4A

    SL4A的全称为Scripting Layer for Android, 顾名思义就是Android的脚本架构层,它的目的就是可以用熟知的脚本开发语言来开发Android应用程序.其工作原理基于RPC ...

  8. [翻译] ADPopupView 触摸弹出视窗

    ADPopupView 触摸弹出视窗 https://github.com/Antondomashnev/ADPopupView ADPopupView is an iOS drop-in class ...

  9. 《Windows核心编程》第2章——字符和字符处理

    ANSI和UNICODE 计算char和wchar_t的长度都一样,都是5,但是二者在内存中的布局实际上是不同的:

  10. [Android Pro] Android源码编译后,再重编译所需要做的清理工作

    1.在源码目录的根目录下,make clean; 2.进到源码的\linux\kernel\目录下,执行make mrproper: 3.再退回到根目录,执行source  build/envsetu ...