Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add has method that supports paths #2

Merged
merged 1 commit into from

2 participants

@mgevans

Thanks for deep-model. I've started using it with backbone.modelbinding, so I needed a has method on models that works with paths. Tests have been added as well.

@powmedia powmedia merged commit 02fd667 into from
@CalebGilbert CalebGilbert referenced this pull request
Open

Joining forces #14

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 10, 2012
  1. Add has method that support paths.

    Matthew Evans authored
This page is out of date. Refresh to see the latest.
Showing with 23 additions and 0 deletions.
  1. +5 −0 src/deep-model.js
  2. +18 −0 test/deep-model.test.js
View
5 src/deep-model.js
@@ -142,6 +142,11 @@
return this;
},
+ // Override has
+ has: function(attr) {
+ return getNested(this.attributes, attr) != null;
+ },
+
// Remove an attribute from the model, firing `"change"` unless you choose
// to silence it. `unset` is a noop if the attribute doesn't exist.
View
18 test/deep-model.test.js
@@ -193,6 +193,24 @@ test("set: Don't convert Date objects to strings", function() {
ok(_.isDate(model.attributes.date));
});
+test("has: Check if model has root key", function(){
+ var model = create();
+
+ equal(model.has('user'), true);
+});
+
+test("has: Check if model has deep key", function(){
+ var model = create();
+
+ equal(model.has('user.name.last'), true);
+});
+
+test("has: Don't find nonexistent key", function(){
+ var model = create();
+
+ equal(model.has('user.turtleneck'), false);
+});
+
test("unset: Unset a root key", function(){
var model = create();
Something went wrong with that request. Please try again.