2015-09-07 23:53:43 +02:00
|
|
|
/*
|
|
|
|
* vim: ts=4:sw=4:expandtab
|
2015-10-01 19:54:49 +02:00
|
|
|
*
|
|
|
|
* Whisper.View
|
|
|
|
*
|
|
|
|
* This is the base for most of our views. The Backbone view is extended
|
|
|
|
* with some conveniences:
|
|
|
|
*
|
|
|
|
* 1. Pre-parses all our mustache templates for performance.
|
|
|
|
* https://github.com/janl/mustache.js#pre-parsing-and-caching-templates
|
|
|
|
*
|
|
|
|
* 2. Defines a default definition for render() which allows sub-classes
|
|
|
|
* to simply specify a templateName and renderAttributes which are plugged
|
|
|
|
* into Mustache.render
|
|
|
|
*
|
|
|
|
* 3. Makes all the templates available for rendering as partials.
|
|
|
|
* https://github.com/janl/mustache.js#partials
|
|
|
|
*
|
|
|
|
* 4. Provides some common functionality, e.g. confirmation dialog
|
|
|
|
*
|
2015-03-06 00:25:49 +01:00
|
|
|
*/
|
|
|
|
(function () {
|
|
|
|
'use strict';
|
|
|
|
window.Whisper = window.Whisper || {};
|
|
|
|
|
|
|
|
Whisper.View = Backbone.View.extend({
|
|
|
|
constructor: function() {
|
|
|
|
Backbone.View.apply(this, arguments);
|
|
|
|
Mustache.parse(_.result(this, 'template'));
|
|
|
|
},
|
2015-03-07 02:05:36 +01:00
|
|
|
render_attributes: function() {
|
2015-03-06 00:25:49 +01:00
|
|
|
return _.result(this.model, 'attributes', {});
|
|
|
|
},
|
2015-03-23 22:01:18 +01:00
|
|
|
render_partials: function() {
|
2015-09-03 22:22:52 +02:00
|
|
|
return Whisper.View.Templates;
|
2015-03-23 22:01:18 +01:00
|
|
|
},
|
2015-09-04 22:06:17 +02:00
|
|
|
template: function() {
|
|
|
|
if (this.templateName) {
|
|
|
|
return Whisper.View.Templates[this.templateName];
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
},
|
2015-03-06 00:25:49 +01:00
|
|
|
render: function() {
|
2015-03-07 02:05:36 +01:00
|
|
|
var attrs = _.result(this, 'render_attributes', {});
|
2015-03-06 00:25:49 +01:00
|
|
|
var template = _.result(this, 'template', '');
|
2015-03-23 22:01:18 +01:00
|
|
|
var partials = _.result(this, 'render_partials', '');
|
|
|
|
this.$el.html(Mustache.render(template, attrs, partials));
|
2015-03-06 00:25:49 +01:00
|
|
|
return this;
|
2015-03-23 22:01:18 +01:00
|
|
|
},
|
2015-05-27 01:54:21 +02:00
|
|
|
confirm: function(message) {
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
var dialog = new Whisper.ConfirmationDialogView({
|
|
|
|
message: message,
|
|
|
|
resolve: resolve,
|
|
|
|
reject: reject
|
|
|
|
});
|
|
|
|
this.$el.append(dialog.el);
|
|
|
|
}.bind(this));
|
|
|
|
}
|
2015-09-03 22:22:52 +02:00
|
|
|
},{
|
|
|
|
// Class attributes
|
|
|
|
Templates: (function() {
|
|
|
|
var templates = {};
|
|
|
|
$('script[type="text/x-tmpl-mustache"]').each(function(i, el) {
|
|
|
|
var $el = $(el);
|
2015-09-04 22:06:17 +02:00
|
|
|
var id = $el.attr('id');
|
2015-09-03 22:22:52 +02:00
|
|
|
templates[id] = $el.html();
|
|
|
|
});
|
|
|
|
return templates;
|
|
|
|
}())
|
2015-03-06 00:25:49 +01:00
|
|
|
});
|
|
|
|
})();
|