[Pkg-javascript-commits] [backbone] 28/211: If Backbone.View#el is a string, pass it through $(...).get(0) in _ensureElement

Jonas Smedegaard js at moszumanska.debian.org
Sat May 3 16:59:58 UTC 2014


This is an automated email from the git hooks/post-receive script.

js pushed a commit to tag 0.5.0
in repository backbone.

commit 71c98381f0d6066030a9a37b7802ee3d13e9a4d4
Author: Sam Stephenson <sam at 37signals.com>
Date:   Fri Dec 17 12:33:12 2010 -0600

    If Backbone.View#el is a string, pass it through $(...).get(0) in _ensureElement
---
 backbone.js  | 18 +++++++++++++-----
 test/view.js | 24 +++++++++++++++++++++++-
 2 files changed, 36 insertions(+), 6 deletions(-)

diff --git a/backbone.js b/backbone.js
index f24ff30..a3b288e 100644
--- a/backbone.js
+++ b/backbone.js
@@ -879,12 +879,20 @@
     },
 
     // 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) return;
-      var attrs = {};
-      if (this.id) attrs.id = this.id;
-      if (this.className) attrs["class"] = this.className;
-      this.el = this.make(this.tagName, attrs);
+      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);
+      }
     }
 
   });
diff --git a/test/view.js b/test/view.js
index 60be9b8..f67df41 100644
--- a/test/view.js
+++ b/test/view.js
@@ -56,7 +56,7 @@ $(document).ready(function() {
     equals(counter2, 3);
   });
 
-  test("View: _ensureElement", function() {
+  test("View: _ensureElement with DOM node el", function() {
     var ViewClass = Backbone.View.extend({
       el: document.body
     });
@@ -64,6 +64,28 @@ $(document).ready(function() {
     equals(view.el, document.body);
   });
 
+  test("View: _ensureElement with string el", function() {
+    var ViewClass = Backbone.View.extend({
+      el: "body"
+    });
+    var view = new ViewClass;
+    equals(view.el, document.body);
+
+    ViewClass = Backbone.View.extend({
+      el: "body > h2"
+    });
+    view = new ViewClass;
+    equals(view.el, $("#qunit-banner").get(0));
+
+    ViewClass = Backbone.View.extend({
+      el: "#nonexistent"
+    });
+    view = new ViewClass;
+    ok(view.el);
+    equals(view.el.tagName.toLowerCase(), "div");
+    equals(view.el.parentNode, null);
+  });
+
   test("View: multiple views per element", function() {
     var count = 0, ViewClass = Backbone.View.extend({
       el: $("body"),

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/backbone.git



More information about the Pkg-javascript-commits mailing list