diff --git a/backbone.js b/backbone.js index 4217e625..60f06d21 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 dbb3d5a4..65ea4fe8 100644 --- a/index.html +++ b/index.html @@ -168,6 +168,7 @@
  • constructor / initialize
  • get
  • escape
  • +
  • is
  • set
  • unset
  • clear
  • @@ -559,6 +560,13 @@ var hacker = new Backbone.Model({ alert(hacker.escape('name')); +

    + ismodel.is(attribute) +
    + Returns whether an attribute is set to a truthy value or not. For example: + note.get("title") +

    +

    setmodel.set(attributes, [options])
    diff --git a/test/model.js b/test/model.js index 87e12e6e..00f53b49 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);