2018-07-07 00:48:14 +00:00
|
|
|
/* global Backbone, Whisper, _ */
|
|
|
|
|
|
|
|
// eslint-disable-next-line func-names
|
2018-04-27 21:25:04 +00:00
|
|
|
(function() {
|
|
|
|
'use strict';
|
2018-07-07 00:48:14 +00:00
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
window.Whisper = window.Whisper || {};
|
2014-07-22 18:55:26 +00:00
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
/*
|
2020-01-08 17:44:54 +00:00
|
|
|
* Generic list view that watches a given collection, wraps its members in
|
|
|
|
* a given child view and adds the child view elements to its own element.
|
|
|
|
*/
|
2018-04-27 21:25:04 +00:00
|
|
|
Whisper.ListView = Backbone.View.extend({
|
|
|
|
tagName: 'ul',
|
|
|
|
itemView: Backbone.View,
|
2018-07-07 00:48:14 +00:00
|
|
|
initialize(options) {
|
2018-04-27 21:25:04 +00:00
|
|
|
this.options = options || {};
|
|
|
|
this.listenTo(this.collection, 'add', this.addOne);
|
|
|
|
this.listenTo(this.collection, 'reset', this.addAll);
|
|
|
|
},
|
2014-07-22 18:55:26 +00:00
|
|
|
|
2018-07-07 00:48:14 +00:00
|
|
|
addOne(model) {
|
2018-04-27 21:25:04 +00:00
|
|
|
if (this.itemView) {
|
2018-07-07 00:48:14 +00:00
|
|
|
const options = _.extend({}, this.options.toInclude, { model });
|
|
|
|
// eslint-disable-next-line new-cap
|
|
|
|
const view = new this.itemView(options);
|
2018-04-27 21:25:04 +00:00
|
|
|
this.$el.append(view.render().el);
|
|
|
|
this.$el.trigger('add');
|
|
|
|
}
|
|
|
|
},
|
2014-07-22 18:55:26 +00:00
|
|
|
|
2018-07-07 00:48:14 +00:00
|
|
|
addAll() {
|
2018-04-27 21:25:04 +00:00
|
|
|
this.$el.html('');
|
|
|
|
this.collection.each(this.addOne, this);
|
|
|
|
},
|
2015-02-10 23:27:26 +00:00
|
|
|
|
2018-07-07 00:48:14 +00:00
|
|
|
render() {
|
2018-04-27 21:25:04 +00:00
|
|
|
this.addAll();
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
});
|
2014-07-22 18:55:26 +00:00
|
|
|
})();
|