Slide 5
Slide 5 text
ENSURE DOM ELEMENT EXISTS
- Backbone will lazy evaluate the string ‘el’
// 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
// an element from the `id`, `className` and `tagName`
proeprties.
_ensureElement : function() {
if (!this.el) {
var attrs = this.attributes || {};
if (this.id) attrs.id = this.id;
if (this.className) attrs['class'] = this.className;
this.el = this.make(this.tagName, attrs);
} else if (_.isString(this.el)) {
this.el = $(this.el).get(0);
}
}
(Backbone source)