[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. ...
随机推荐
- 在JSP使用EL中判断指定元素是否存在于指定集合中
在JSP使用EL中判断指定元素是否存在于指定集合中 1.问题描述 在JSP页面中使用EL表达式判断一个指定元素是否存在于指定集合中? 2.问题解决 eg:指定集合:collection:{1,2,3, ...
- 【20181031T1】一串数字【分解质因数+贪心】
题面 [错解] 立方就是所有质因子次数都是3的倍数嘛 发现1e5的三次根很小,可以枚举所有和这个数乘起来是完全立方数的(flag*1) 然后--连条边跑最大独立集? 不对啊是NP问题(实际上是个二分图 ...
- bzoj 3996 最小割
公式推出来后想了半天没思路,居然A是01矩阵..... 如果一个问题是求最值,并那么尝试先将所有可能收益加起来,然后矛盾部分能否用最小割表达(本题有两个矛盾,第一个是选还是不选,第二个是i,j有一个不 ...
- java泛型中的E,K,V,T,U,S
注释: java 泛型类型使用大写形式,且比较短,这是常见的 在java库中,使用变量 E 表示集合的元素类型 K 和 V 分别表示数据库表数据的键key和值value的类型 T(如果有需要还可以使用 ...
- git 的补丁使用方法
1.生成补丁 format-patch可以基于分支进行打包,也可以基于上几次更新内容打包. 基于上几次内容打包 git format-patch HEAD^ 有几个^就会打几个patch,从最近一次 ...
- 使用socket.io+redis来实现基本的聊天室应用场景
本文根据socket.io与Redis来实现基本的聊天室应用场景,主要表现于多个浏览器之间的信息同步和实时更新. 只是简单记录了一下, 更详细的内容可以参考后续的一篇补充文章: 使用node.js + ...
- 通过 ssh 登录到手机 Termux
通过ssh登录到手机 Termux 测试环境 电脑: macOS Mojave 手机: Huawei Mate10Pro Termux是Android上的一个非常强大的终端模拟器. 强大之处在于支持使 ...
- MySql篇
CentOS6下通过yum安装的MySQL是5.1版的,比较老,所以就想通过源代码安装高版本的5.6.26. 一:卸载旧版本 使用下面的命令检查是否安装有MySQL Server rpm -qa | ...
- 最好的PHP博客系统
1.Wordpress http://www.wordpress.org/ B2基础上开发而来,这是国内用户比较喜欢而且用户较多的一个php博客程序,缘由是因为Wordpress提供大量插件和模板,让 ...
- The sigrok project
http://www.sigrok.org/wiki/Main_Page The sigrok project aims at creating a portable, cross-platform, ...