Make it easier to reference templates

Whisper.Views can now use templateName to reference the id of the
desired template.
This commit is contained in:
lilia 2015-09-04 13:06:17 -07:00
parent 239b87a1fa
commit e402338af7
4 changed files with 11 additions and 5 deletions

View file

@ -19,7 +19,7 @@
var ContactView = Whisper.View.extend({ var ContactView = Whisper.View.extend({
className: 'contact-detail', className: 'contact-detail',
template: $('#contact-detail').html(), templateName: 'contact-detail',
initialize: function(options) { initialize: function(options) {
this.conflict = options.conflict; this.conflict = options.conflict;
}, },

View file

@ -20,7 +20,7 @@
Whisper.NewGroupUpdateView = Whisper.View.extend({ Whisper.NewGroupUpdateView = Whisper.View.extend({
tagName: "div", tagName: "div",
className: "new-group-update-form", className: "new-group-update-form",
template: $('#new-group-update-form').html(), templateName: 'new-group-update',
initialize: function(options) { initialize: function(options) {
this.render(); this.render();
this.avatarInput = new Whisper.FileInputView({ this.avatarInput = new Whisper.FileInputView({

View file

@ -38,7 +38,7 @@
events: { events: {
'click .remove': 'removeModel' 'click .remove': 'removeModel'
}, },
template: $('#contact_pill').html(), templateName: 'contact_pill',
initialize: function() { initialize: function() {
var error = this.model.validate(this.model.attributes); var error = this.model.validate(this.model.attributes);
if (error) { if (error) {
@ -60,7 +60,7 @@
Whisper.RecipientsInputView = Whisper.View.extend({ Whisper.RecipientsInputView = Whisper.View.extend({
className: 'recipients-input', className: 'recipients-input',
template: $('#recipients-input').html(), templateName: 'recipients-input',
initialize: function(options) { initialize: function(options) {
if (options) { if (options) {
this.placeholder = options.placeholder; this.placeholder = options.placeholder;

View file

@ -28,6 +28,12 @@
render_partials: function() { render_partials: function() {
return Whisper.View.Templates; return Whisper.View.Templates;
}, },
template: function() {
if (this.templateName) {
return Whisper.View.Templates[this.templateName];
}
return '';
},
render: function() { render: function() {
var attrs = _.result(this, 'render_attributes', {}); var attrs = _.result(this, 'render_attributes', {});
var template = _.result(this, 'template', ''); var template = _.result(this, 'template', '');
@ -51,7 +57,7 @@
var templates = {}; var templates = {};
$('script[type="text/x-tmpl-mustache"]').each(function(i, el) { $('script[type="text/x-tmpl-mustache"]').each(function(i, el) {
var $el = $(el); var $el = $(el);
var id = $el.attr('id').replace('-','_'); var id = $el.attr('id');
templates[id] = $el.html(); templates[id] = $el.html();
}); });
return templates; return templates;