mirror of
https://github.com/jashkenas/backbone.git
synced 2026-04-08 03:00:26 -04:00
Adding Model#has to Backbone...
This commit is contained in:
@@ -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
|
||||
|
||||
57
index.html
57
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>
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user