mirror of
https://github.com/jashkenas/backbone.git
synced 2026-01-25 06:48:07 -05:00
Merge branch 'Model-is' of https://github.com/mtodd/backbone
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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>
|
||||
@@ -559,6 +560,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 />
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user