From 2e32c7bbc3ab7adeb11dbb8391c300acaa98fc34 Mon Sep 17 00:00:00 2001 From: lilia Date: Wed, 21 Oct 2015 10:52:20 -0700 Subject: [PATCH] Small refactor of attachment views Bind the sub-view to some data when we initialize it, rather than passing it in on render. That means the image view click handler will only ever open the blob we bound it to, even if its src attr changes for some reason, which should never happen, but if it does, it's nice to guard against opening arbitrary urls found in the dom. // FREEBIE --- js/views/attachment_view.js | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/js/views/attachment_view.js b/js/views/attachment_view.js index 1a577d2b3d..ece0a6ccb0 100644 --- a/js/views/attachment_view.js +++ b/js/views/attachment_view.js @@ -6,6 +6,9 @@ var ImageView = Backbone.View.extend({ tagName: 'img', + initialize: function(dataUrl) { + this.dataUrl = dataUrl; + }, events: { 'load': 'update', 'click': 'open' @@ -14,16 +17,18 @@ this.$el.trigger('update'); }, open: function () { - window.open(this.$el.attr('src'), '_blank'); + window.open(this.dataUrl, '_blank'); }, - render: function(dataUrl) { - this.$el.attr('src', dataUrl); + render: function() { + this.$el.attr('src', this.dataUrl); return this; } }); var MediaView = Backbone.View.extend({ - initialize: function() { + initialize: function(dataUrl, contentType) { + this.dataUrl = dataUrl; + this.contentType = contentType; this.$el.attr('controls', ''); }, events: { @@ -32,10 +37,10 @@ update: function() { this.$el.trigger('update'); }, - render: function(dataUrl, contentType) { + render: function() { var $el = $(''); - $el.attr('src', dataUrl); - $el.attr('type', contentType); + $el.attr('src', this.dataUrl); + $el.attr('type', this.contentType); this.$el.append($el); return this; } @@ -48,17 +53,18 @@ tagName: 'span', className: 'attachment', render: function() { - var view; + var View; switch(this.model.contentType.split('/')[0]) { - case 'image': view = new ImageView(); break; - case 'audio': view = new AudioView(); break; - case 'video': view = new VideoView(); break; + case 'image': View = ImageView; break; + case 'audio': View = AudioView; break; + case 'video': View = VideoView; break; default: throw 'Unsupported attachment type'; } - view.$el.appendTo(this.$el); var blob = new Blob([this.model.data], {type: this.model.contentType}); - view.render(window.URL.createObjectURL(blob), this.model.contentType); + var view = new View(window.URL.createObjectURL(blob), this.model.contentType); + view.$el.appendTo(this.$el); + view.render(); return this; } });