[Backbone]6. Collections.
Define a collection:
var AppointmentList = Backbone.Collection.extend({model: Appointment});
RESET the json:
var appointments = new AppointmentList();
var json = [
{title: 'Back pain'},
{title: 'Dry mouth'},
{title: 'Headache'}
];
appointments.reset(json);
Get url and fetch json data:
var AppointmentList = Backbone.Collection.extend({
url: '/appointments',
model: Appointment
});
var appointments = new AppointmentList();
appointments.fetch();
This Dr. Goodparts does not trust us when we tell him we are successfully loading data from the server into our collection.
To prove him wrong, display an alert with the number of items in the collection by listening for the reset event.
var appointments = new AppointmentList();
appointments.fetch();
appointments.on('reset', function(){
alert(appointments.length);
});
Wouldn't ya know, our users don't like getting alerts every time we fetch new data for our collection.
Update the fetch call below to not fire the reset event.
//By default, when fetch or add model, the reset event will be triggered.
//TO disable the reset event, can use
appointments.fetch({silent: true});
Use an event listener to log to the console the model's title anytime a model is added to the appointments collection.
var appointments = new AppointmentList();
appointments.on('add', function(appointment){
console.log(appointment.get('title'));
});
There are a lot of appointments in our collection and Dr. Goodparts wants a list of all appointment titles so he can arrange his equipment for the day.
Use the map iteration function to return an array of appointment titles and assign to the titles variable.
var titles = new Array();
appointments.map(function(appoinment){
titles.push(appoinment.get('title'));
});
[Backbone]6. Collections.的更多相关文章
- Backbone.js 为复杂Javascript应用程序提供模型(models)、集合(collections)、视图(views)的结构
Backbone.js 为复杂Javascript应用程序提供模型(models).集合(collections).视图(views)的结构.其中模型用于绑定键值数据和 自定义事件:集合附有可枚举函数 ...
- 前端MVC框架Backbone 1.1.0源码分析(一)
前言 如何定义库与框架 前端的辅助工具太多太多了,那么我们是如何定义库与框架? jQuery是目前用的最广的库了,但是整体来讲jQuery目的性很也明确针对“DOM操作”,当然自己写一个原生态方法也能 ...
- TodoMVC中的Backbone+MarionetteJS+RequireJS例子源码分析之二 数据处理
当我们使用jQuery时大部分时间是聚焦于Dom节点的处理,给Dom节点绑定事件等等:前端mvc框架backbone则如何呢? M-Model,Collection等,是聚焦于数据的处理,它把与后台数 ...
- TodoMVC中的Backbone+MarionetteJS+RequireJS例子源码分析之一
Marionette牵线木偶,Backbone是脊骨的意思,Marionette是基于Backbone做扩展库,可以理解为把脊骨骨架绑线扯着变成牵线木偶动起来哈哈,使backbone更易使用呵呵! 构 ...
- Django+Tastypie作后端,RequireJS+Backbone作前端的TodoMVC
一.配置好环境 接着前一篇的例子,顺带测试一下已下载下来example里面的backbone_require的例子 注意:直接本地用backbone.localStorage插件运行TodoMVC会报 ...
- Django+Tastypie作后端,Backbone作前端的TodoMVC
TodoMVC是各种js框架入门的比较经典的例子,详细可查看github地址https://github.com/tastejs/todomvc 接着上篇文章, 1,先在github上把backbon ...
- MVVM与Backbone demo
MVVM https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93viewmodel
- Backbone的一点使用心得
Backbone的其实感觉上上手很难,大概在一年前就想实践下,结果总是没有付诸行动,这次需求中狠狠心决定一定要使用一次看看,感受下. 可是第一步真的比较困难,因为直接看API好像没有感觉就在网上找实例 ...
- Backbone.js学习之Backbone.View(视图)
Backbone.js为复杂WEB应用程序提供模型(models).集合(collections).视图(views)的结构.其中模型用于绑定键值数据和自定义事件:集合附有可枚举函数的丰富API: 视 ...
随机推荐
- hdu 4531 bfs(略难)
题目链接:点我 第一次不太清楚怎么判重,现在懂了,等下次再做 /* *HDU 4531 *BFS *注意判重 */ #include <stdio.h> #include <stri ...
- 计算机二级软件VC++6.0下载地址
计算机二级软件VC++6.0介绍: 适合所有参加全国计算机等级考试的童鞋们……见图如下: 下载地址:(以下两者任选其一即可) (1).计算机二级软件VC++6.0(16.35MB) (2).计算机二级 ...
- Codeforces Round #357 (Div. 2) B. Economy Game 水题
B. Economy Game 题目连接: http://www.codeforces.com/contest/681/problem/B Description Kolya is developin ...
- Codeforces Round #356 (Div. 2) A. Bear and Five Cards 水题
A. Bear and Five Cards 题目连接: http://www.codeforces.com/contest/680/problem/A Description A little be ...
- [转载] 使用Kettle进行数据迁移(ETL)
由于开发新的系统,需要将之前一个老的C/S应用的数据按照新的数据设计导入到新库中.此过程可能涉及到表结构不一致.大数据量(千万级,甚至上亿)等情况,包括异构数据的抽取.清洗等等工作.部分复杂的工作需要 ...
- 使用yum高速部署Oracle安装环境(11g)
基于Linux安装过Oracle的童鞋们都应该清楚,安装Oracle的确是一件比較费时费力的差事,由于不过前期的rpm包,内核參数,创建用户等等这些个步骤都让那些新手不免眼花缭乱,一不留神.就导致终于 ...
- 5V and 3V Level Translators
http://www.daycounter.com/Circuits/Level-Translators/Level-Translators.phtml Interfacing 5V and 3V l ...
- 腾讯旗下网站的很多URL都包含“cgi-bin”,是什么意思?他们后台用什么语言?
cgi-bin 这很有可能说明后台是C/C++写的. 动态Web技术刚出来的时候, 服务器调用本地应用程序处理http请求的技术. 通常是C/C++程序. 后来有了新的web开发技术后这类用的就比较少 ...
- C# 去掉webapi返回json所带的转义字符
反序列换报错: {"Error converting value \"{\"Result\":true,\"Code\":\"\& ...
- Pig系统分析(5)-从Logical Plan到Physical Plan
Physical Plan生成过程 优化后的逻辑运行计划被LogToPhyTranslationVisitor处理,生成物理运行计划. 这是一个经典的Vistor设计模式应用场景. 当中,LogToP ...