[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: 视 ...
随机推荐
- 从DOS时代至移动互联网的技术路线回顾
从DOS时代至移动互联网的技术路线回顾 Normal 0 7.8 磅 0 2 false false false EN-US ZH-CN X-NONE /* Style Definitions */ ...
- 【贪心】Codeforces Round #480 (Div. 2) C. Posterized
题意:让你对[0,255]这个序列任意划分成一些不重叠的子段,每个子段的大小不超过K.给你n个不超过255的数,让你将每个数替换成它所在子段的任意一个元素,使得最终这个n个数的序列的字典序最小. p[ ...
- Elasticsearch中document的基础知识
写在前面的话:读书破万卷,编码如有神-------------------------------------------------------------------- 参考内容: <Ela ...
- BZOJ 3876: [Ahoi2014]支线剧情 带下界的费用流
3876: [Ahoi2014]支线剧情 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=3876 Description [故事背景] 宅 ...
- POJ 3320 Jessica's Reading Problem 尺取法/map
Jessica's Reading Problem Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7467 Accept ...
- 关于php一些问题
为什么说php是弱语言? 本身不严格区分变量的类型. 为什么说php是动态语言? 程序在运行时可以改变其结构.所谓的动态类型语言,意思就是类型的检查是在运行时做的. 为什么说php是脚本语言? 不需要 ...
- ASK,OOK,FSK,GFSK是什么
http://www.21say.com/askookfskgfsk%E6%98%AF%E4%BB%80%E4%B9%88/ ASK是幅移键控调制的简写,例如二进制的,把二进制符号0和1分别用不同的幅 ...
- 11gR2 RAC重新启动后仅仅能起单节点
11gR2 RAC重新启动后仅仅能起单节点 问题背景: 将11gR2 RAC正常部署完毕之后运行两节点重新启动操作发现当中有一个节点的集群资源无法启动,遂再次重新启动该无法启动集群资源的节点,还是不可 ...
- 绝对定位的div的居中方法,下面的写法兼容IE系列浏览器和火狐浏览器。
详细解说,直接看样式:#dingwei{padding:10px;background-color:#003300;color:#FFFFFF; width:600px;height:300px; d ...
- Error : The specified component was not reported by the VSS writer (Error 517) in Windows Server 2012 Backup
Error : The specified component was not reported by the VSS writer (Error 517) in Windows Server 201 ...