Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add templateAsEl option to View #3259

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ const ClassOptions = [
'regionClass',
'regions',
'template',
'templateAsEl',
'templateContext',
'triggers',
'ui'
];

const TEMPLATE_AS_EL = 'TEMPLATE_AS_EL';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if for some reason this PR does go through, we should look into changing this into a Symbol()


// The standard view. Includes view events, automatic rendering
// of Underscore templates, nested views, and more.
const View = Backbone.View.extend({
Expand All @@ -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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
},

Expand Down