Skip to content

Loading…

Add `difference` underscore.js method to Backbone.Collection #2491

Closed
wants to merge 4 commits into from

3 participants

@mikefrey

Adds the difference underscore.js method to Collection, allowing for this

var arr = _.difference(myCollection.models, excludeThese)

to become this

var arr = myCollection.difference(excludeThese)

I added a test for it as well, which passes. However, when I forked off master, a failing test already existed.

@wookiehangover
Collaborator

@mikefrey thanks for opening a pull request.

What's the real world use case behind this? I'm curious because it doesn't seem like all that common of a pattern...

@mikefrey

Hi @wookiehangover
Thanks for the quick response!

My primary use-case is presenting items to add from one group to another, excluding the items in first group that are already in the second (adding users into groups for example). That being said, I can see difference having the same use-cases as without, which is already included in Collection.

@wookiehangover
Collaborator

@mikefrey I know it's been a little while since you opened this, but would it be possible for you to push a version of this with that merges cleanly with master?

@mikefrey

@wookiehangover I've updated my branch, but it looks like difference was already added to master. I suppose you could still merge this for the tests.

@caseywebdev
Collaborator

@mikefrey we actually already have a test for difference as well, but thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Showing with 2 additions and 1 deletion.
  1. +2 −1 test/collection.js
View
3 test/collection.js
@@ -503,7 +503,7 @@ $(document).ready(function() {
equal(coll.findWhere({a: 4}), void 0);
});
- test("Underscore methods", 14, function() {
+ test("Underscore methods", 15, function() {
equal(col.map(function(model){ return model.get('label'); }).join(' '), 'a b c d');
equal(col.any(function(model){ return model.id === 100; }), false);
equal(col.any(function(model){ return model.id === 0; }), true);
@@ -514,6 +514,7 @@ $(document).ready(function() {
ok(_.include(col.rest(), d));
ok(!col.isEmpty());
ok(!_.include(col.without(d), d));
+ ok(!_.include(col.difference([d]), d));
equal(col.max(function(model){ return model.id; }).id, 3);
equal(col.min(function(model){ return model.id; }).id, 0);
deepEqual(col.chain()
Something went wrong with that request. Please try again.