[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,所以昨 ...
随机推荐
- bzoj 3997 Dilworth定理
看到这道题感觉像是网络流,如果没有权值,可以用DAG最小路径覆盖,有权值,感觉可以求一个上下界最小可行流,但内存卡了....时间估计也悬. 正解要用到一些数学知识,这里梳理一下: 定义: 偏序关系: ...
- tyvj 1044 数字三角形 记忆化搜索
数字三角形 Time Limit: 1 Sec Memory Limit: 162 MB 题目连接 http://www.tyvj.cn/p/1044 Description 示出了一个数字三角形. ...
- sql中如何统计一字段中字符串的个数
declare @s varchar(100)set @s='156434A27kAsdABCiosd-01&**('--找出现的次数select len(@s)-len(replace(@s ...
- 工作记录(1)- js问题
也是好久不写博客了,确实懒了:想想应该把node.js的东西写完整比较好,在抽时间吧: 这几天在做阿里巴巴的一个页面展示,里面设计到了一些js的问题,中途也遇到了一些幼稚的问题, 算是简单记录一下,以 ...
- Hello World on Impala
Cloudera Impala 官方教程 <Impala Tutorial>,解说了Impala一些基本操作,但操作步骤前后缺少连贯性,本文节W选<Impala Tutorial&g ...
- 《Android学习指南》文件夹
转自:http://android.yaohuiji.com/about Android学习指南的内容分类: 分类 描写叙述 0.学习Android必备的Java基础知识 没有Java基础的朋友,请不 ...
- nginx做TCP代理实现群集
nginx做TCP代理实现群集 nginx从版本1.9开始,既能做HTTP代理,又能做TCP代理,这就非常完美了. 配置nginx.conf. 为了简单起见,笔者故意去掉了HTTP代理配置部分,只保留 ...
- 【C#高级编程】笔记之核心C#
Main()方法 每一个C#可执行文件(如控制台程序.Windows程序和Windows服务)都必须有一个入口点——Main()方法(注意M大写). 这个方法必须是类或静态方法,并且返回类型必须是in ...
- Nginx+Memcached+Tomcat集群配置实践(Sticky Session)
准备工作 创建一个简单的web应用,名为session.其中有两个页面,分别如下所示: 页面login.jsp <%@ page language="java" conten ...
- 使用IP访问Mantis显示空白页的解决办法
使用http://localhost/mantis/ 可成功访问Mantis,但使用IP地址:http://172.16.20.111/Mantis却访不了,显示“无法显示网页”. 在aphache中 ...