signal-desktop/js/views/list_view.js

38 lines
993 B
JavaScript
Raw Normal View History

2018-04-27 21:25:04 +00:00
(function() {
'use strict';
window.Whisper = window.Whisper || {};
2018-04-27 21:25:04 +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,
initialize: function(options) {
this.options = options || {};
this.listenTo(this.collection, 'add', this.addOne);
this.listenTo(this.collection, 'reset', this.addAll);
},
2018-04-27 21:25:04 +00:00
addOne: function(model) {
if (this.itemView) {
var options = _.extend({}, this.options.toInclude, { model: model });
var view = new this.itemView(options);
this.$el.append(view.render().el);
this.$el.trigger('add');
}
},
2018-04-27 21:25:04 +00:00
addAll: function() {
this.$el.html('');
this.collection.each(this.addOne, this);
},
2018-04-27 21:25:04 +00:00
render: function() {
this.addAll();
return this;
},
});
})();