[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. ...
随机推荐
- servlet生命周期和执行流程
一 .生命周期 servlet 声明周期可以分四个阶段: 类装载过程 init() 初始化过程 service() 服务过程,选择doGet \ doPost destroy() 销毁过程 servl ...
- python调用matlab
官网链接: https://ww2.mathworks.cn/help/matlab/matlab_external/call-user-script-and-function-from-python ...
- 使用ViewPager实现android软件使用向导的功能
现在的大部分android软件,都是使用说明,就是第一次使用该软件时,会出现向导,可以左右滑动,然后就进入应用的主界面了. 先看下效果图: 首先需要一个布局文件,是FlameLayout组成的,里面包 ...
- SQLyog客户端无法连接MySQL服务器
环境:centos下使用yum 命令安装了mysql服务 1.进入linux 通过命令service mysqld start启动mysql的服务 2.使用sqlyog 连接mysql发现连接不上,如 ...
- private、protected、public
private protected public 本类内 Y Y Y 子类内 N Y Y 外部 N N N <?php class MyClass{ public $public = 'Pu ...
- Git_工作区和暂存区
Git和其他版本控制系统如SVN的一个不同之处就是有暂存区的概念. 先来看名词解释. 工作区(Working Directory):就是你在电脑里能看到的目录,比如我的learngit文件夹就是一个工 ...
- org.springframework.orm.hibernate3.LocalSessionFactoryBean
Spring整合hibernate在配置sessionFactory时, 启动总是报出javax.transaction.TransactionManager找不到. 原因是:缺少jar包,jta-1 ...
- 基于curl的异步http实现
简述用于windowsclient的一个异步http模块的实现 1.须要实现的feature 1.1 非常easy地发起异步http请求,然后回调. 1.2 可以管理http并发数. 1.3 可以支持 ...
- [实时更新]jquery全部版本号下载
jquery-2.1.0 注!不再支持IE 6/7/8 直接引用地址: 开发版地址1: <script src="http://code.jquery.com/jquery-2. ...
- Python kmean
# -*- coding: utf-8 -*-from sklearn.cluster import KMeansfrom sklearn.externals import joblibimport ...