[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: 视 ...
随机推荐
- android studio 汉化 个性化 美化 快速操作项目 目录
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 汉化包 百度云盘 下载地址:https://pan.baidu.com/s/1pLjwy ...
- android Handler机制 消息机制
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 循环器Looper 管理该线程内对象之间的消息交换 messageExchange 循 ...
- 【POJ】1419:Graph Coloring【普通图最大点独立集】【最大团】
Graph Coloring Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5775 Accepted: 2678 ...
- Sql 先进先出计算积分
先建表,插入测试数据 --正积分表 CREATE table tb1 ( ) NOT NULL, ) NOT NULL, ) NULL, [point] [int] NULL ) ) ) ) ) ) ...
- 在IDEA(phpStorm)中使用Babel编译ES6
安装Babel 官方文档建议我们根据单个项目进行本地安装,原因是不同的项目可以依赖不同版本的 Babel,使你的项目更方便移植.更易于安装. 在项目的根目录下使用命令行工具(CMD等)执行下面代码 n ...
- Detecting Underlying Linux Distro
If you are the owner of the system, then you know which Linux is installed and running. This article ...
- Docker 1.3 公布
Docker 1.3 公布 Docker 1.3 已经正式公布.新的特性包含镜像签名.进程注入.新的创建和执行容器命令.安全选项和 Mac OS 上进行文件夹共享.特别是针对安全方面的改进,成为本地公 ...
- error launching remote program failed to get the task for process
Error Starting executable: error launching remote program failed to get the task for process 715 这个 ...
- Hadoop 组成
这里介绍一下hadoop的组成, hadoop主要由两部分组成,,一个是hdfs,还有一个是mapreduce 这两个部分在hadoop 2.2.0中分别用start-dfs.sh和start-yar ...
- CASpringAnimation的使用
CASpringAnimation的使用 效果 源码 https://github.com/YouXianMing/Animations // // CASpringAnimationControll ...