[Backbone]7. Collection Views, Custom Events
It's finally time to start building out our Appointment app. We're going to be using a collection and a collection view to display a list of appointments to the ornery but brilliant Dr. Goodparts.
Let's start by creating a View Class named AppointmentListView and then create an instance of that class, passing in our collection instance appointments
var Appointment = Backbone.Model.extend({}); var AppointmentList = Backbone.Collection.extend({
model: Appointment
});
Answer:
var appointments = new AppointmentList();
var AppointmentListView = Backbone.View.extend({});
var appointmentListView = new AppointmentListView({collection: appointments });
Good morning. Last night you were so close to implementing the render function on AppointmentListView
but decided to take a nap, and here you are!
Go ahead and implement the addOne function, rendering an AppointmentView
for each model in the collection, and appending it to the collection view's top-level element.
Note There is a bug in the forEach
call. Make sure and fix it before submitting.
var Appointment = Backbone.Model.extend({});
var AppointmentList = Backbone.Collection.extend({
model: Appointment
});
var AppointmentView = Backbone.View.extend({
template: _.template('<span class="<%= if(cancelled){
print("cancelled");
} %>" + '<%= title %></span>'> + '<a href="#">x</a>'), render: function(){
this.$el.html(this.tamplate(this.model.toJSON()));
return this;
}
});
Answer:
var AppointmentListView = Backbone.View.extend({
render: function(){
this.collection.forEach(this.addOne,this);
},
addOne: function(model){
var appointmentView = new AppointmentView({model: model});
this.$el.append(appointmentView.render().el);
}
});
Wow, you are a hard worker! Let's see it pay off by rendering our collection view and inserting it into the DOM. Using theappend
or html
jQuery methods, insert the top-level element into the #app
div.
var appointmentsView = new AppointmentListView({collection: appointmentList});
$("#app").append(appointmentsView.render().el);
Uh oh. Dr. Goodparts is at it again, adding new models to the collection, and he's upset because when he adds a model, the DOM isn't updated.
In the AppointmentListView's
initialize
function, listen for the collections add
event and call the addOne
function to append the new model to the view.
Make sure you include the context argument to ensure addOne
is called with the correct context.
var AppointmentListView = Backbone.View.extend({
initialize: function(){
this.collection.on('add', this.addOne, this);
},
render: function(){
this.collection.forEach(this.addOne, this);
},
addOne: function(model){
var appointmentView = new AppointmentView({model: model});
appointmentView.render();
this.$el.append(appointmentView.el);
}
});
It's Monday morning and time to reset all the appointments for the week. You hear a screech from down the hall and seconds later Dr. Goodparts barges red-faced into your office because the DOM didn't update when he reset the collection.
Update the AppointmentListView to listen for the collection's reset
event to call the render
function.
Make sure you include the context argument to ensure render
is called with the correct context.
var AppointmentListView = Backbone.View.extend({
initialize: function(){
this.collection.on('add', this.addOne, this);
this.collection.on('reset', this.render, this);
},
render: function(){
this.collection.forEach(this.addOne, this);
},
addOne: function(model){
var appointmentView = new AppointmentView({model: model});
appointmentView.render();
this.$el.append(appointmentView.el);
}
});
Turns out one of the appointments in our collection was rescheduled for next week, but when Dr. Goodparts removed the appointment model from the collection, it wasn't removed from the DOM. You can imagine Dr. Goodparts reaction.
Fix this bug by using a custom event hide
on Appointment
models.
Update your AppointmentList
collection to trigger the hide
event on a model when it is removed.
Update your AppointmentView
to call the remove
function whenever its model fires the hide
event.
Appointment_list.js
var AppointmentList = Backbone.Collection.extend({
model: Appointment,
initialize: function(){
this.on('remove', this.hideModel);
},
hideModel: function(model){
model.trigger('hide');
}
});
Appointment_view.js
var AppointmentView = Backbone.View.extend({
initialize: function(){
this.model.on('hide', this.remove, this);
},
remove: function(){
this.$el.remove();
}
});
[Backbone]7. Collection Views, Custom Events的更多相关文章
- Advanced Collection Views and Building Custom Layouts
Advanced Collection Views and Building Custom Layouts UICollectionView的结构回顾 首先回顾一下Collection View的构成 ...
- Collection Views and Building Custom Layouts-备
UICollectionView的结构回顾 首先回顾一下Collection View的构成,我们能看到的有三个部分: Cells Supplementary Views 追加视图 (类似Header ...
- Collection View Programming Guide for iOS---(一)----About iOS Collection Views
Next About iOS Collection Views 关于iOS Collection Views A collection view is a way to present an orde ...
- EventEmitter & custom events & sub/pub
EventEmitter & custom events & sub/pub https://repl.it/@xgqfrms/PPLabs-frontend-answers // 5 ...
- -_-#【Backbone】Collection
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- backbone之collection
最近要用到backbone.js,网上也找了些资料,但是就看到一个开头还可以,往下看基本就看不下去了,幸好有这本书[LeanpubRead] Backbone.Marionette.js A Gent ...
- backbone.js 教程(1) View & Model & Collection
Backbone.js Overview 它由Jeremy Ashkenas开发,最初发行于2010-10-13 它是一个轻量的JavaScript类库,只依赖于underscore.js,非强制依赖 ...
- Backbone源码解析(三):Collection模块
Collection模块式是对分散在项目中model的收集,他可以存储所有的model,构成一个集合,并且通过自身的方法统一操作model.Collection模块包装着若干对象,对象本身不具有一些方 ...
- 【再探backbone 02】集合-Collection
前言 昨天我们一起学习了backbone的model,我个人对backbone的熟悉程度提高了,但是也发现一个严重的问题!!! 我平时压根没有用到model这块的东西,事实上我只用到了view,所以昨 ...
随机推荐
- 「HNOI2018」游戏
「HNOI2018」游戏 解题思路 首先没有锁上的门可以缩点缩掉,然后对于一扇锁上的门,如果钥匙在左边,那么右边就永远不可能到达左边,同理如果钥匙在右边,左边就永远不可能到达右边. 然后考虑一个暴力的 ...
- disable_functions php-fpm root
php.ini disable_functions 禁用某些函数 需要时注意打开 php-fpm 对应conf user group为root时 ERROR: [pool www] please sp ...
- 2017-2018-1 20162307 Dijkstra算法
2017-2018-1 20162307 Dijkstra算法 题目要求 Dijkstra算法,求解附图顶点A的单源最短路径 在纸上画出求解过程,上传截图(注意图上要有自己的学号和姓名) 解题步骤
- Go Web编程 第四章--处理请求
请求和响应 Request结构 URL字段 Header字段 Body字段 Form, PostForm, MultipartForm字段 在处理器函数中,我们可以通过Request获取各个字段的详细 ...
- java的反射机制(第三篇)
本文转载自:http://c.biancheng.net/cpp/html/1782.html Person p=new Person();这是什么?当然是实例化一个对象了.可是这种实例化对象的方法存 ...
- django官方文档读书笔记
写在前面:这算是第二次读英文原文文档,第一次是读scrapy,感觉还是要做笔记,好记性不如烂笔头,现在已经忘了scrapy文档讲了什么了,心疼.以后要多读多写 经过半年的基础学习(懒,拖延)终于来到w ...
- bzoj 3545/3551: [ONTAK2010]Peaks -- 主席树,最小生成树,倍增
3545: [ONTAK2010]Peaks Time Limit: 10 Sec Memory Limit: 128 MB Description 在Bytemountains有N座山峰,每座山峰 ...
- 电子数据识别软件ABBYY FineReader
ABBYY 是一家俄罗斯软件公司,在文档识别,数据捕获和语言技术的开发中居世界领先地位.其获奖产品 FineReader OCR 软件可以把静态纸文件和 PDF 文件转换成可管理的电子数据,可以大大节 ...
- iOS开发里的Bundle是个啥玩意?!
初学iOS开发的同学,不管是自己写的,还是粘贴的代码,或多或少都写过下面的代码 [[NSBundle mainBundle] pathForResource:@"someFileName&q ...
- 关于OPC Client 编写2
最近在搞到一个OPC动态库OPCAutomation.dll,该动态库在http://www.kepware.com/可下载,下面介绍如何用C#进行OPC Client开发. 1.新建C#应用程序,命 ...