Let message models manage blob urls for attachments
// FREEBIE
This commit is contained in:
parent
04c8796bd3
commit
59313b5177
1 changed files with 28 additions and 0 deletions
|
@ -8,6 +8,10 @@
|
|||
var Message = window.Whisper.Message = Backbone.Model.extend({
|
||||
database : Whisper.Database,
|
||||
storeName : 'messages',
|
||||
initialize: function() {
|
||||
this.on('change:attachments', this.updateImageUrl);
|
||||
this.on('destroy', this.revokeImageUrl);
|
||||
},
|
||||
defaults : function() {
|
||||
return {
|
||||
timestamp: new Date().getTime(),
|
||||
|
@ -57,6 +61,30 @@
|
|||
|
||||
return this.get('body');
|
||||
},
|
||||
updateImageUrl: function() {
|
||||
this.revokeImageUrl();
|
||||
var attachment = message.get('attachments')[0];
|
||||
if (attachment) {
|
||||
var blob = new Blob([attachment.data], {
|
||||
type: attachment.contentType
|
||||
});
|
||||
this.imageUrl = URL.createObjectURL(blob);
|
||||
} else {
|
||||
this.imageUrl = null;
|
||||
}
|
||||
},
|
||||
revokeImageUrl: function() {
|
||||
if (this.imageUrl) {
|
||||
URL.revokeObjectURL(this.imageUrl);
|
||||
this.imageUrl = null;
|
||||
}
|
||||
},
|
||||
getImageUrl: function() {
|
||||
if (this.imageUrl === undefined) {
|
||||
this.updateImageUrl();
|
||||
}
|
||||
return this.imageUrl;
|
||||
},
|
||||
getContact: function() {
|
||||
if (this.collection) {
|
||||
return this.collection.conversation.contactCollection.get(this.get('source'));
|
||||
|
|
Loading…
Reference in a new issue