diff --git a/src/view.js b/src/view.js index 506c36a3f4..1e63bf06f7 100644 --- a/src/view.js +++ b/src/view.js @@ -20,11 +20,14 @@ const ClassOptions = [ 'regionClass', 'regions', 'template', + 'templateAsEl', 'templateContext', 'triggers', 'ui' ]; +const TEMPLATE_AS_EL = 'TEMPLATE_AS_EL'; + // The standard view. Includes view events, automatic rendering // of Underscore templates, nested views, and more. const View = Backbone.View.extend({ @@ -36,6 +39,10 @@ const View = Backbone.View.extend({ this.mergeOptions(options, ClassOptions); + if (this.templateAsEl) { + this.el = TEMPLATE_AS_EL; + } + monitorViewEvents(this); this._initBehaviors(); @@ -86,7 +93,9 @@ const View = Backbone.View.extend({ // Overriding Backbone.View's `setElement` to handle // if an el was previously defined. If so, the view might be // rendered or attached on setElement. - setElement() { + setElement(el) { + if (el === TEMPLATE_AS_EL) { return this; } + const hasEl = !!this.el; Backbone.View.prototype.setElement.apply(this, arguments); @@ -145,6 +154,12 @@ const View = Backbone.View.extend({ // Render and add to el const html = Renderer.render(template, data, this); + + if (this.templateAsEl) { + this.setElement(html); + return; + } + this.attachElContent(html); },