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 addOnefunction 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的更多相关文章

  1. Advanced Collection Views and Building Custom Layouts

    Advanced Collection Views and Building Custom Layouts UICollectionView的结构回顾 首先回顾一下Collection View的构成 ...

  2. Collection Views and Building Custom Layouts-备

    UICollectionView的结构回顾 首先回顾一下Collection View的构成,我们能看到的有三个部分: Cells Supplementary Views 追加视图 (类似Header ...

  3. 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 ...

  4. EventEmitter & custom events & sub/pub

    EventEmitter & custom events & sub/pub https://repl.it/@xgqfrms/PPLabs-frontend-answers // 5 ...

  5. -_-#【Backbone】Collection

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  6. backbone之collection

    最近要用到backbone.js,网上也找了些资料,但是就看到一个开头还可以,往下看基本就看不下去了,幸好有这本书[LeanpubRead] Backbone.Marionette.js A Gent ...

  7. backbone.js 教程(1) View & Model & Collection

    Backbone.js Overview 它由Jeremy Ashkenas开发,最初发行于2010-10-13 它是一个轻量的JavaScript类库,只依赖于underscore.js,非强制依赖 ...

  8. Backbone源码解析(三):Collection模块

    Collection模块式是对分散在项目中model的收集,他可以存储所有的model,构成一个集合,并且通过自身的方法统一操作model.Collection模块包装着若干对象,对象本身不具有一些方 ...

  9. 【再探backbone 02】集合-Collection

    前言 昨天我们一起学习了backbone的model,我个人对backbone的熟悉程度提高了,但是也发现一个严重的问题!!! 我平时压根没有用到model这块的东西,事实上我只用到了view,所以昨 ...

随机推荐

  1. ubuntu18.04 安装Navicat 解决字体方框问题

    前景 最近带着看一点数据库的知识,装一下navicat,就是这个玩意儿,在我编码毫无问题的情况下,这个软件上却显示各种乱码 环境 ubuntu 18.04 navicat 12(最新版) mysql ...

  2. windows提权的几种姿势

    想象这种画面:你拿到了一台机器上Meterpreter会话了,然后你准备运行 getsystem 命令进行提权,但如果提权没有成功,你就准备认输了吗?只有懦夫才会认输.但是你不是,对吗?你是一个勇者! ...

  3. 【洛谷】4180:【模板】严格次小生成树[BJWC2010]【链剖】【线段树维护最大、严格次大值】

    P4180 [模板]严格次小生成树[BJWC2010] 题目描述 小C最近学了很多最小生成树的算法,Prim算法.Kurskal算法.消圈算法等等.正当小C洋洋得意之时,小P又来泼小C冷水了.小P说, ...

  4. poj 3164

    朱刘算法 步骤: 1.计算出每个点边权最小的边的权(如果除根以外有其他的点没有入边,则不存在最小树形图),并记下边的另一个端点(称其为这个点的前趋) 2.沿着每个点向上走,如果在走到根或环上的点之前, ...

  5. mysql另类分页方法

    mysql> limit ,;select found_rows(); +----+-----------------+---------+--------+ | id | rankName | ...

  6. spring---transaction(3)---源代码分析(事务的管理器PlatformTransactionManager)

    写在前面 由于实现事务功能的方式各不相同,Spring进行了统一的抽象,形成了PlatformTransactionManager事务管理器顶级接口(平台事务管理器),事务的提交.回滚等操作全部交给它 ...

  7. 回顾下$.ajax()方法参数

    1.url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type: 要求为String类型的参数,请求方式(post或get)默认为get.注意其他http请求方法,例如 ...

  8. PHP获取文件大小详解

    通过PHP filesize函数可直接获取文件大小(单位字节),如:filesize('test.png') echo filesize('test.png'); 查看test.png图片属性: 文件 ...

  9. PHP常量定义之define与const对比

    简要归纳PHP中两个常量定义关键字的区别: 1.define是函数,const是语言结构,PHP编译时const速度快.2.define只能用在类外,const类内类外皆可.3.define定义的常量 ...

  10. oracle-systemtap

    https://github.com/hatem-mahmoud/scripts https://mahmoudhatem.wordpress.com/2015/05/26/stapora-v1-0- ...