[Pkg-javascript-commits] [backbone] 112/173: add `has` method to collection

Jonas Smedegaard dr at jones.dk
Wed Aug 31 07:44:09 UTC 2016


This is an automated email from the git hooks/post-receive script.

js pushed a commit to branch master
in repository backbone.

commit 75666b08c91bfc399e878023f82dc1fbb14ce883
Author: Craig Martin <cmartin at onshift.com>
Date:   Sun Jan 17 12:19:36 2016 -0500

    add `has` method to collection
---
 backbone.js        |  5 +++++
 test/collection.js | 20 ++++++++++++++++++++
 2 files changed, 25 insertions(+)

diff --git a/backbone.js b/backbone.js
index 7e0dd47..6ea5e81 100644
--- a/backbone.js
+++ b/backbone.js
@@ -973,6 +973,11 @@
       return this._byId[obj] || this._byId[id] || this._byId[obj.cid];
     },
 
+    // Returns `true` if the model is in the collection.
+    has: function(obj) {
+      return this.get(obj) != null;
+    },
+
     // Get the model at the given index.
     at: function(index) {
       if (index < 0) index += this.length;
diff --git a/test/collection.js b/test/collection.js
index eba6526..bf95581 100644
--- a/test/collection.js
+++ b/test/collection.js
@@ -106,6 +106,26 @@
     assert.equal(collection.get(1).id, 1);
   });
 
+  QUnit.test('has', function(assert) {
+    assert.expect(15);
+    assert.ok(col.has(a));
+    assert.ok(col.has(b));
+    assert.ok(col.has(c));
+    assert.ok(col.has(d));
+    assert.ok(col.has(a.id));
+    assert.ok(col.has(b.id));
+    assert.ok(col.has(c.id));
+    assert.ok(col.has(d.id));
+    assert.ok(col.has(a.cid));
+    assert.ok(col.has(b.cid));
+    assert.ok(col.has(c.cid));
+    assert.ok(col.has(d.cid));
+    var outsider = new Backbone.Model({id: 4});
+    assert.notOk(col.has(outsider));
+    assert.notOk(col.has(outsider.id));
+    assert.notOk(col.has(outsider.cid));
+  });
+
   QUnit.test('update index when id changes', function(assert) {
     assert.expect(4);
     var col = new Backbone.Collection();

-- 
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