[Backbone]2. More detail in Models
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的更多相关文章
- [Backbone]3. More detail on View
Change the AppointmentView to have a top-level li tag (instead of the default div tag). var Appointm ...
- RequireJS与Backbone简单整合
前言 昨天我们一起学习了Backbone,最后做了一个备忘录的例子,说是做了不如说是看了下官方提供的例子,所以最终我感觉我们还是没能掌握Backbone,今天还得做个其它例子先. 然后前面也只是草草学 ...
- Backbone入门讲解
Backbone是一个实现了web前端mvc模式的js框架. 一种解决问题的通用方法,我们叫做模式. 设计模式:工厂模式,适配器模式,观察者模式等,推荐js设计模式这本书.设计模式是一种思想. 框架模 ...
- 【转】Backbone使用总结
转自 http://www.pchou.info/javascript/2014/06/26/backbone-summary-01.html 开始在项目中大规模使用backbone,一路磕磕碰碰, ...
- [backbone] Getting Started with Backbone.js
一.简介 Backbone 是一个 JavaScript MVC 框架,它属于轻量级框架,且易于学习掌握.模型.视图.集合和路由器从不同的层面划分了应用程序,并负责处理几种特定事件.处理 Ajax 应 ...
- Backbone入门
Backbone入门讲解 Backbone是一个实现了web前端mvc模式的js框架. 一种解决问题的通用方法,我们叫做模式. 设计模式:工厂模式,适配器模式,观察者模式等,推荐js设计模式这本书.设 ...
- 开始学习 Backbone
[转]开始学习 Backbone 如何将模型-视图-控制器 (MVC) 架构引入 Ajax Web 应用程序 如何高效管理 Web 应用程序中的数目众多的 JavaScript 代码行是一个挑战.As ...
- [Backbone] First Application!!!!
Important things to remember: 1. Usually, we create Collection, CollectionViews, Model, View. Collec ...
- backbone测试代码
一.入门测试 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www. ...
随机推荐
- 线性表之顺序表C++实现
线性表之顺序表 一.头文件:SeqList.h //顺序线性表的头文件 #include<iostream> ; //定义顺序表SeqList的模板类 template<class ...
- CF893F Subtree Minimum Query 主席树
如果是求和就很好做了... 不是求和也无伤大雅.... 一维太难限制条件了,考虑二维限制 一维$dfs$序,一维$dep$序 询问$(x, k)$对应着在$dfs$上查$[dfn[x], dfn[x] ...
- bzoj 3997 Dilworth定理
看到这道题感觉像是网络流,如果没有权值,可以用DAG最小路径覆盖,有权值,感觉可以求一个上下界最小可行流,但内存卡了....时间估计也悬. 正解要用到一些数学知识,这里梳理一下: 定义: 偏序关系: ...
- 20172333 2017-2018-2 《Java程序设计》第6周学习总结
20172333 2017-2018-2 <Java程序设计>第6周学习总结 教材学习内容 1.数组的基本用法,如数组的定义:int[该数组类型] name = new int[X]X为数 ...
- sql中如何统计一字段中字符串的个数
declare @s varchar(100)set @s='156434A27kAsdABCiosd-01&**('--找出现的次数select len(@s)-len(replace(@s ...
- HashMap和Hashtable的区别--List,Set,Map等接口是否都继承自Map接口--Collection和Collections的区别
面试题: 1.HashMap和Hashtable的区别? HashMap:线程不安全,效率高,键和值都允许null值 Hashtable:线程安全,效率低,键和值都不允许null值 ArrayList ...
- Go语言Web框架gwk介绍 (三)
上一篇忘了ChanResult ChanResult 可以用来模拟BigPipe,定义如下 type ChanResult struct { Wait sync.WaitGroup Chan chan ...
- ng-show和ng-if的区别
第一点区别是, ng-if 在后面表达式为 true 的时候才创建这个 dom 节点, ng-show 是初始时就创建了,用display:block 和 display:none 来控制显示和不显示 ...
- 忘记Oracle系统管理员sys的密码
sqlplus / as sysdba; alter user sys identified by newpwd***;
- Disable File System Redirector For Windows x64 (Python recipe)(转)
This disables the Windows File System Redirector.When a 32 bit program runs on a 64 bit operating sy ...