mirror of
https://github.com/jashkenas/backbone.git
synced 2026-04-30 03:00:06 -04:00
Merging Issue #149. View#el can be a string.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
28
index.html
28
index.html
@@ -1656,17 +1656,31 @@ new DocumentRow({
|
||||
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>
|
||||
|
||||
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user