[Pkg-javascript-commits] [backbone] 13/211: Adding Model#has to Backbone...
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 0c1bbbcc97a1995885d685225094ab8e4f881b28
Author: Jeremy Ashkenas <jashkenas at gmail.com>
Date: Wed Dec 8 12:23:17 2010 -0500
Adding Model#has to Backbone...
---
backbone.js | 7 ++++---
index.html | 57 ++++++++++++++++++++++++++++++++-------------------------
test/model.js | 21 +++++++++++----------
3 files changed, 47 insertions(+), 38 deletions(-)
diff --git a/backbone.js b/backbone.js
index 60f06d2..5907a17 100644
--- a/backbone.js
+++ b/backbone.js
@@ -158,9 +158,10 @@
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;
+ // Returns `true` if the attribute contains a value that is not null
+ // or undefined.
+ has : function(attr) {
+ return this.attributes[attr] != null;
},
// Set a hash of model attributes on the object, firing `"change"` unless you
diff --git a/index.html b/index.html
index 65ea4fe..ffc2b3a 100644
--- a/index.html
+++ b/index.html
@@ -167,9 +167,9 @@
<li>– <a href="#Model-extend">extend</a></li>
<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-escape">escape</a></li>
+ <li>– <a href="#Model-has">has</a></li>
<li>– <a href="#Model-unset">unset</a></li>
<li>– <a href="#Model-clear">clear</a></li>
<li>– <a href="#Model-id">id</a></li>
@@ -543,29 +543,6 @@ new Book({
<tt>note.get("title")</tt>
</p>
- <p id="Model-escape">
- <b class="header">escape</b><code>model.escape(attribute)</code>
- <br />
- Similar to <a href="#Model-get">get</a>, but returns the HTML-escaped version
- of a model's attribute. If you're interpolating data from the model into
- HTML, using <b>escape</b> to retrieve attributes will prevent
- <a href="http://en.wikipedia.org/wiki/Cross-site_scripting">XSS</a> attacks.
- </p>
-
-<pre class="runnable">
-var hacker = new Backbone.Model({
- name: "<script>alert('xss')</script>"
-});
-
-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>
@@ -589,6 +566,36 @@ note.set({title: "October 12", content: "Lorem Ipsum Dolor Sit Amet..."});
callback in the options, which will be invoked instead of triggering an
<tt>"error"</tt> event, should validation fail.
</p>
+
+ <p id="Model-escape">
+ <b class="header">escape</b><code>model.escape(attribute)</code>
+ <br />
+ Similar to <a href="#Model-get">get</a>, but returns the HTML-escaped version
+ of a model's attribute. If you're interpolating data from the model into
+ HTML, using <b>escape</b> to retrieve attributes will prevent
+ <a href="http://en.wikipedia.org/wiki/Cross-site_scripting">XSS</a> attacks.
+ </p>
+
+<pre class="runnable">
+var hacker = new Backbone.Model({
+ name: "<script>alert('xss')</script>"
+});
+
+alert(hacker.escape('name'));
+</pre>
+
+ <p id="Model-has">
+ <b class="header">has</b><code>model.has(attribute)</code>
+ <br />
+ Returns <tt>true</tt> if the attribute is set to a non-null or non-undefined
+ value.
+ </p>
+
+<pre>
+if (note.has("title")) {
+ ...
+}
+</pre>
<p id="Model-unset">
<b class="header">unset</b><code>model.unset(attribute, [options])</code>
diff --git a/test/model.js b/test/model.js
index 00f53b4..6827997 100644
--- a/test/model.js
+++ b/test/model.js
@@ -104,18 +104,19 @@ $(document).ready(function() {
equals(doc.escape('audience'), '');
});
- test("Model: is", function() {
- attrs = { 'foo': 1 };
+ test("Model: has", function() {
+ attrs = {};
a = new Backbone.Model(attrs);
- // falsiness
- _([false, null, undefined, '', 0]).each(function(value) {
- a.set({'foo': value});
- equals(a.is("foo"), false);
+ equals(a.has("name"), false);
+ _([true, "Truth!", 1, false, '', 0]).each(function(value) {
+ a.set({'name': value});
+ equals(a.has("name"), true);
});
- // truthiness
- _([true, "Truth!", 1]).each(function(value) {
- a.set({'foo': value});
- equals(a.is("foo"), true);
+ a.unset('name');
+ equals(a.has('name'), false);
+ _([null, undefined]).each(function(value) {
+ a.set({'name': value});
+ equals(a.has("name"), false);
});
});
--
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