Skip to content

Commit

Permalink
Merging Issue jashkenas#149. View#el can be a string.
Browse files Browse the repository at this point in the history
  • Loading branch information
jashkenas committed Dec 17, 2010
1 parent 71c9838 commit 12f7ae9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
6 changes: 2 additions & 4 deletions backbone.js
Expand Up @@ -883,15 +883,13 @@
// matching element, and re-assign it to `el`. Otherwise, create
// an element from the `id`, `className` and `tagName` proeprties.
_ensureElement : function() {
if (_.isString(this.el)) {
this.el = $(this.el).get(0);
}

if (!this.el) {
var attrs = {};
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);
}
}

Expand Down
28 changes: 21 additions & 7 deletions index.html
Expand Up @@ -1656,17 +1656,31 @@ <h2 id="View">Backbone.View</h2>
whether they've already been inserted into the page or not. In this
fashion, views can be rendered at any time, and inserted into the DOM all
at once, in order to get high-performance UI rendering with as few
reflows and repaints as possible.
reflows and repaints as possible. <tt>this.el</tt> is created from the
view's <tt>tagName</tt>, <tt>className</tt>, and <tt>id</tt> properties,
if specified. If not, <b>el</b> is an empty <tt>div</tt>.
</p>

<p>
<tt>this.el</tt> is created from the view's <tt>tagName</tt>, <tt>className</tt>,
and <tt>id</tt> properties, if specified. If not, <b>el</b> is an empty <tt>div</tt>.
You may assign <b>el</b> directly in your view's
<a href="View-constructor">initialize</a> function, if the view is being
created for an element that already exists in the DOM. Make sure to assign
a real DOM element, and not a jQuery object.
You may assign <b>el</b> directly if the view is being
created for an element that already exists in the DOM. Use either a
reference to a real DOM element, or a css selector string.
</p>

<pre class="runnable">
var ItemView = Backbone.View.extend({
tagName: 'li'
});

var BodyView = Backbone.View.extend({
el: 'body'
});

var item = new ItemView();
var body = new BodyView();

alert(item.el + ' ' + body.el);
</pre>

<p id="View-dollar">
<b class="header">$ (jQuery or Zepto)</b><code>view.$(selector)</code>
Expand Down
4 changes: 1 addition & 3 deletions test/view.js
Expand Up @@ -81,9 +81,7 @@ $(document).ready(function() {
el: "#nonexistent"
});
view = new ViewClass;
ok(view.el);
equals(view.el.tagName.toLowerCase(), "div");
equals(view.el.parentNode, null);
ok(!view.el);
});

test("View: multiple views per element", function() {
Expand Down

0 comments on commit 12f7ae9

Please sign in to comment.