DRY up a common view pattern

Define a Whisper.View base class that automatically parses and renders
templates and attributes defined by the subclass. This saves us a good
number of lines of code as well as some marginal memory overhead, since
we are no longer saving per-instance copies of template strings.
This commit is contained in:
lilia 2015-03-05 15:25:49 -08:00
parent 7c9ad975bb
commit 1bb480f6ea
16 changed files with 109 additions and 60 deletions

View file

@ -17,15 +17,11 @@ var Whisper = Whisper || {};
(function () {
'use strict';
Whisper.AttachmentPreviewView = Backbone.View.extend({
Whisper.AttachmentPreviewView = Whisper.View.extend({
className: 'attachment-preview',
initialize: function() {
this.template = $('#attachment-preview').html();
Mustache.parse(this.template);
},
render: function() {
this.$el.html(Mustache.render(this.template, {source: this.src}));
return this;
template: $('#attachment-preview').html(),
attributes: function() {
return {source: this.src};
}
});
})();