Our Appointment model doesn't seem too useful yet. Add two default attributes, title as the string "Checkup", anddate which should default to the current time new Date().

var Appointment = Backbone.Model.extend({
defaults:{
"title": "Checkup",
"date": new Date()
}
});

While messing around in the console, you've discovered a strange bug. Every new Appointment you create has the same exact date, instead of the date and time of when the Appointment instance was created.

This is because new Date() is evaluated once, when the Appointment model is first created, and not re-evaluated every time a new instance is created.

To fix this you'll want to assign a function to defaults instead of just a plain object. Wrap the object below in a function which returns the object. This will cause the defaults to get evaluated every time a new instance is created.

var Appointment = Backbone.Model.extend({
defaults : function(){
return {
title: 'Checkup',
date: new Date
}
}
});

Dr. Goodparts finally ponied up for a server and has seeded it with his first few appointments. Luckily for us, he bought the REST package with the JSON add-on.

Point the root URL of your Appointment model to the /appointments endpoint.

Then, create a new Appointment with an id of 1, and fetch its data from the server.

var Appointment = Backbone.Model.extend({urlRoot: '/appointments'});
var appointment = new Appointment({id: 1});
appointment.fetch();

Setting the urlRoot of the Appointment model lets us do more than just fetch from the server, it also lets us sync changes made to model instances.

Dr. Goodparts isn't feeling good today so we're going to have to cancel his appointments. Set the appointment'scancelled attribute to true and save the appointment to the server.

var appointment = new Appointment({id: 1});
appointment.set("cancelled", true);
//save it to server.
appointment.save();

Dr. Goodparts is upset that he wasn't notified when we changed his last appointment to cancelled.

Add a listener to the appointment model instance to pop-up an alert box (using alert) whenever any of the model attributes change.

var appointment = new Appointment({id: 1});
appointment.on('change', function(){
alert("Appointment cancelled!");
});

Dr. Goodparts browser crashed because of too many alerts.

Instead of listening for all attribute changes, just listen and alert when changes are made to the cancelled attribute.

appointment.on('change:cancelled', function(){
alert("Hey Dr. Goodparts, your appointment has changed!");
});

We've already seen how we can use get to access attributes on a model instance, but what if we wanted them all at once?

Use the console.log function to log the JSON of the appointment instance using toJSON. If you don't remember howtoJSON works, consult the Backbone Model docs.

var appointment = new Appointment({id: 1});
console.log(appointment.toJSON());

-----------Final Code--------------

//Create a model CLASS
var Appointment = Backbone.Model.extend({
defaults : function(){
return {
title: 'Checkup',
date: new Date()
}
}
});
//Define a object for model
var Appointment = Backbone.Model.extend({urlRoot: '/appointments'});
var appointment = new Appointment({id: 1});
appointment.fetch();
console.log(appointment.toJSON());
/*
appointment.on('change', function(){
alert("Hey Dr. Goodparts, your appointment has changed!");
});*/
appointment.on('change:cancelled', function(){
alert("Hey Dr. Goodparts, your appointment has changed!");
});
/*
If you want to set attribute
appointment.set("cancelled", true);
//save it to server.
appointment.save();
*/
//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]2. More detail in Models的更多相关文章

  1. [Backbone]3. More detail on View

    Change the AppointmentView to have a top-level li tag (instead of the default div tag). var Appointm ...

  2. RequireJS与Backbone简单整合

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

  3. Backbone入门讲解

    Backbone是一个实现了web前端mvc模式的js框架. 一种解决问题的通用方法,我们叫做模式. 设计模式:工厂模式,适配器模式,观察者模式等,推荐js设计模式这本书.设计模式是一种思想. 框架模 ...

  4. 【转】Backbone使用总结

    转自  http://www.pchou.info/javascript/2014/06/26/backbone-summary-01.html 开始在项目中大规模使用backbone,一路磕磕碰碰, ...

  5. [backbone] Getting Started with Backbone.js

    一.简介 Backbone 是一个 JavaScript MVC 框架,它属于轻量级框架,且易于学习掌握.模型.视图.集合和路由器从不同的层面划分了应用程序,并负责处理几种特定事件.处理 Ajax 应 ...

  6. Backbone入门

    Backbone入门讲解 Backbone是一个实现了web前端mvc模式的js框架. 一种解决问题的通用方法,我们叫做模式. 设计模式:工厂模式,适配器模式,观察者模式等,推荐js设计模式这本书.设 ...

  7. 开始学习 Backbone

    [转]开始学习 Backbone 如何将模型-视图-控制器 (MVC) 架构引入 Ajax Web 应用程序 如何高效管理 Web 应用程序中的数目众多的 JavaScript 代码行是一个挑战.As ...

  8. [Backbone] First Application!!!!

    Important things to remember: 1. Usually, we create Collection, CollectionViews, Model, View. Collec ...

  9. backbone测试代码

    一.入门测试 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www. ...

随机推荐

  1. [Luogu5161]WD与数列(后缀数组/后缀自动机+线段树合并)

    https://blog.csdn.net/WAautomaton/article/details/85057257 解法一:后缀数组 显然将原数组差分后答案就是所有不相交不相邻重复子串个数+n*(n ...

  2. HDU 4641 K-string 后缀自动机 并查集

    http://acm.hdu.edu.cn/showproblem.php?pid=4641 https://blog.csdn.net/asdfgh0308/article/details/4096 ...

  3. 洛谷.4252.[NOI2006]聪明的导游(提答 直径 随机化)

    题目链接 随机化 暴力: 随便从一个点开始DFS,每次从之前得到的f[i]最大的子节点开始DFS.f[i]为从i开始(之前)能得到的最大答案. 要注意的是f[i]应当有机会从更小的答案更新, 9.10 ...

  4. [Agc002E]Candy Piles

    [Agc002E]Candy Piles 题目大意 有\(n\)个数,两人轮流操作,可以做以下操作之一: 删掉一个最大的数 将所有数-1 最后取没的人输,问先手是否必胜? 试题分析 直接决策不知道选哪 ...

  5. CentOS系统下中文文件名乱码

    原文来自:http://www.zhukun.net/archives/7434 CentOS系统下中文文件名乱码 2014/09/01Linux运维centos.Linuxbear 从windows ...

  6. 【HDU】2866:Special Prime【数论】

    Special Prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  7. 数据表-java类的映射

    1.一个数据表对应一个java类 2.数据表的字段对应java类的属性 3.一对多的数据表关系 一方用一个java对象表示 多方用一个java对象数组表示 4.多对多的数据表关系:采用中间表,将多对多 ...

  8. 【原】MySQL实用SQL积累

    [文档简述] 本文档用来记录一些常用的SQL语句,以达到快速查询的目的. [常用SQL] 1.mysql数据库中获取某个表的所有字段名 select COLUMN_NAME from informat ...

  9. VHDL语言实现的任意整数分频器

    fpga中,一般外接的晶振是50Mhz,如果电路中一个模块需要25mhz时钟,那么进行一个2分频,这个是相当容易的,下面是一种方法,还有可以用一个二进制计数器实现.这里就不写代码了.easy.同样的原 ...

  10. Android学习网站(1)

    收集了一些比较好的Android学习网站,希望对大家有所帮助: 1.http://developer.android.com/ Android官方网站,可惜被屏蔽了,需要使用FQ软件 2.http:/ ...