2014-05-12 02:13:09 +02:00
|
|
|
var Whisper = Whisper || {};
|
|
|
|
|
|
|
|
(function () {
|
|
|
|
'use strict';
|
|
|
|
|
2014-06-08 00:51:51 +02:00
|
|
|
Whisper.ConversationListView = Backbone.View.extend({
|
2014-05-12 02:13:09 +02:00
|
|
|
|
|
|
|
tagName: 'ul',
|
|
|
|
id: 'conversations',
|
|
|
|
initialize: function() {
|
|
|
|
this.views = [];
|
2014-05-17 06:48:46 +02:00
|
|
|
this.threads = Whisper.Threads;
|
|
|
|
this.listenTo(this.threads, 'change:completed', this.render); // auto update
|
|
|
|
this.listenTo(this.threads, 'add', this.addThread);
|
|
|
|
this.listenTo(this.threads, 'reset', this.addAll);
|
|
|
|
this.listenTo(this.threads, 'all', this.render);
|
2014-06-04 04:37:10 +02:00
|
|
|
this.listenTo(Whisper.Messages, 'add', this.addMessage);
|
2014-05-12 02:13:09 +02:00
|
|
|
|
|
|
|
// Suppresses 'add' events with {reset: true} and prevents the app view
|
|
|
|
// from being re-rendered for every model. Only renders when the 'reset'
|
|
|
|
// event is triggered at the end of the fetch.
|
2014-05-17 06:48:46 +02:00
|
|
|
//this.messages.threads({reset: true});
|
|
|
|
Whisper.Threads.fetch({reset: true});
|
2014-06-04 04:37:10 +02:00
|
|
|
Whisper.Messages.fetch();
|
2014-05-12 02:13:09 +02:00
|
|
|
|
|
|
|
this.$el.appendTo($('#inbox'));
|
2014-05-17 06:48:46 +02:00
|
|
|
},
|
2014-05-12 02:13:09 +02:00
|
|
|
|
2014-05-17 06:48:46 +02:00
|
|
|
addThread: function(thread) {
|
|
|
|
this.views[thread.id] = new Whisper.ConversationView({model: thread});
|
|
|
|
this.$el.prepend(this.views[thread.id].render().el);
|
2014-05-12 02:13:09 +02:00
|
|
|
},
|
|
|
|
|
2014-05-17 06:48:46 +02:00
|
|
|
addAll: function() {
|
2014-05-12 02:13:09 +02:00
|
|
|
this.$el.html('');
|
2014-05-17 06:48:46 +02:00
|
|
|
this.threads.each(this.addThread, this);
|
2014-05-12 02:13:09 +02:00
|
|
|
},
|
2014-06-04 04:37:10 +02:00
|
|
|
|
|
|
|
addMessage: function(message) {
|
|
|
|
message.thread().trigger('message', message);
|
|
|
|
}
|
2014-06-08 00:51:51 +02:00
|
|
|
});
|
2014-05-12 02:13:09 +02:00
|
|
|
})();
|