[Pkg-javascript-commits] [backbone] 09/211: Implement model.is() for attr test, with docs, tests

Jonas Smedegaard js at moszumanska.debian.org
Sat May 3 16:59:57 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 21a3675db926831999096c3e368e1be5a78738a4
Author: Matt Todd <chiology at gmail.com>
Date:   Tue Dec 7 02:39:39 2010 -0500

    Implement model.is() for attr test, with docs, tests
---
 backbone.js   |  5 +++++
 index.html    |  8 ++++++++
 test/model.js | 15 +++++++++++++++
 3 files changed, 28 insertions(+)

diff --git a/backbone.js b/backbone.js
index 4217e62..60f06d2 100644
--- a/backbone.js
+++ b/backbone.js
@@ -158,6 +158,11 @@
       return this._escapedAttributes[attr] = escapeHTML(val == null ? '' : val);
     },
 
+    // Returns true if the attribute evalutates to a truthy value. False otherwise.
+    is : function(attr) {
+      return !!this.attributes[attr] === true;
+    },
+
     // Set a hash of model attributes on the object, firing `"change"` unless you
     // choose to silence it.
     set : function(attrs, options) {
diff --git a/index.html b/index.html
index df6000f..b42423f 100644
--- a/index.html
+++ b/index.html
@@ -168,6 +168,7 @@
       <li>– <a href="#Model-constructor">constructor / initialize</a></li>
       <li>– <a href="#Model-get">get</a></li>
       <li>– <a href="#Model-escape">escape</a></li>
+      <li>– <a href="#Model-is">is</a></li>
       <li>– <a href="#Model-set">set</a></li>
       <li>– <a href="#Model-unset">unset</a></li>
       <li>– <a href="#Model-clear">clear</a></li>
@@ -558,6 +559,13 @@ var hacker = new Backbone.Model({
 alert(hacker.escape('name'));
 </pre>
 
+    <p id="Model-is">
+      <b class="header">is</b><code>model.is(attribute)</code>
+      <br />
+      Returns whether an attribute is set to a truthy value or not. For example:
+      <tt>note.get("title")</tt>
+    </p>
+
     <p id="Model-set">
       <b class="header">set</b><code>model.set(attributes, [options])</code>
       <br />
diff --git a/test/model.js b/test/model.js
index 87e12e6..00f53b4 100644
--- a/test/model.js
+++ b/test/model.js
@@ -104,6 +104,21 @@ $(document).ready(function() {
     equals(doc.escape('audience'), '');
   });
 
+  test("Model: is", function() {
+    attrs = { 'foo': 1 };
+    a = new Backbone.Model(attrs);
+    // falsiness
+    _([false, null, undefined, '', 0]).each(function(value) {
+      a.set({'foo': value});
+      equals(a.is("foo"), false);
+    });
+    // truthiness
+    _([true, "Truth!", 1]).each(function(value) {
+      a.set({'foo': value});
+      equals(a.is("foo"), true);
+    });
+  });
+
   test("Model: set and unset", function() {
     attrs = { 'foo': 1, 'bar': 2, 'baz': 3};
     a = new Backbone.Model(attrs);

-- 
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