From 705aff58a33c563575c278f5f1102d4ea7e704f0 Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Wed, 20 Oct 2010 14:41:50 -0400 Subject: [PATCH] Issue #33. Refactor towards View#_ensureElement --- backbone.js | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/backbone.js b/backbone.js index d53e3f58..a2f43f24 100644 --- a/backbone.js +++ b/backbone.js @@ -515,14 +515,7 @@ // if an existing element is not provided... Backbone.View = function(options) { this._configure(options || {}); - if (this.options.el) { - this.el = this.options.el; - } else { - var attrs = {}; - if (this.id) attrs.id = this.id; - if (this.className) attrs.className = this.className; - this.el = this.make(this.tagName, attrs); - } + this._ensureElement(); if (this.initialize) this.initialize(options); }; @@ -603,10 +596,20 @@ if (this.options) options = _.extend({}, this.options, options); if (options.model) this.model = options.model; if (options.collection) this.collection = options.collection; + if (options.el) this.el = options.el; if (options.id) this.id = options.id; if (options.className) this.className = options.className; if (options.tagName) this.tagName = options.tagName; this.options = options; + }, + + // Ensure that the View has a DOM element to render into. + _ensureElement : function() { + if (this.el) return; + var attrs = {}; + if (this.id) attrs.id = this.id; + if (this.className) attrs.className = this.className; + this.el = this.make(this.tagName, attrs); } });