Skip to content

Commit

Permalink
DOM element creation should be overrideable
Browse files Browse the repository at this point in the history
- Add test for _createElement
- Send tag name to _createElement
  • Loading branch information
nhunzaker committed Mar 20, 2014
1 parent ed3a17d commit a04d7da
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
7 changes: 6 additions & 1 deletion backbone.js
Original file line number Diff line number Diff line change
Expand Up @@ -1145,6 +1145,11 @@
this.$el.off(eventName + '.delegateEvents' + this.cid, selector, listener);
},

// Produces a DOM element according to the `tagName` parameter.
_createElement: function(tagName) {
return document.createElement(tagName);
},

// Ensure that the View has a DOM element to render into.
// If `this.el` is a string, pass it through `$()`, take the first
// matching element, and re-assign it to `el`. Otherwise, create
Expand All @@ -1154,7 +1159,7 @@
var attrs = _.extend({}, _.result(this, 'attributes'));
if (this.id) attrs.id = _.result(this, 'id');
if (this.className) attrs['class'] = _.result(this, 'className');
this.setElement(document.createElement(_.result(this, 'tagName')));
this.setElement(this._createElement(_.result(this, 'tagName')));
this._setAttributes(attrs);
} else {
this.setElement(_.result(this, 'el'));
Expand Down
8 changes: 8 additions & 0 deletions test/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,14 @@
view.$el.trigger('click');
});

test("_createElement produces the correct DOM el", 1, function() {
var View = Backbone.View.extend({
tagName: 'span'
});

equal(new View().el.tagName, 'SPAN');
});

test("_ensureElement with DOM node el", 1, function() {
var View = Backbone.View.extend({
el: document.body
Expand Down

0 comments on commit a04d7da

Please sign in to comment.