From 6d21c05b187639eadea3f41a5eccd616c98f3714 Mon Sep 17 00:00:00 2001 From: Casey Foster Date: Wed, 23 May 2012 08:50:33 -0700 Subject: [PATCH 1/4] Allow Collection to receive falsy `comparator` to override default --- backbone.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backbone.js b/backbone.js index 383ecdd7..08c668af 100644 --- a/backbone.js +++ b/backbone.js @@ -548,7 +548,7 @@ var Collection = Backbone.Collection = function(models, options) { options || (options = {}); if (options.model) this.model = options.model; - if (options.comparator) this.comparator = options.comparator; + if (typeof options.comparator !== 'undefined') this.comparator = options.comparator; this._reset(); this.initialize.apply(this, arguments); if (models) this.reset(models, {silent: true, parse: options.parse}); From 98e01bbc4ad2a0cee44198699de73c898ab4e069 Mon Sep 17 00:00:00 2001 From: Sam Breed Date: Wed, 23 May 2012 23:53:13 -0600 Subject: [PATCH 2/4] adding test for falsy comparators (#1342) --- backbone.js | 2 +- test/collection.js | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/backbone.js b/backbone.js index 08c668af..06a49dd0 100644 --- a/backbone.js +++ b/backbone.js @@ -548,7 +548,7 @@ var Collection = Backbone.Collection = function(models, options) { options || (options = {}); if (options.model) this.model = options.model; - if (typeof options.comparator !== 'undefined') this.comparator = options.comparator; + if (options.comparator !== undefined) this.comparator = options.comparator; this._reset(); this.initialize.apply(this, arguments); if (models) this.reset(models, {silent: true, parse: options.parse}); diff --git a/test/collection.js b/test/collection.js index ceb8e60a..530a1785 100644 --- a/test/collection.js +++ b/test/collection.js @@ -599,4 +599,17 @@ $(document).ready(function() { ok(!collection.get('undefined')); }); + test("Collection: falsy comparator", function(){ + var Col = Backbone.Collection.extend({ + comparator: function(model){ return model.id; } + }); + var col = new Col([{id: 2}, {id: 1}], { + comparator: false + }); + var otherCol = new Col([{id: 2}, {id: 1}]); + raises(function(){ col.sort() }); + equal(col.first().id, 2); + equal(otherCol.first().id, 1); + }); + }); From 4a0acdee506395eaa935d89517bf9fea5c5c209a Mon Sep 17 00:00:00 2001 From: Casey Foster Date: Thu, 24 May 2012 09:02:22 -0700 Subject: [PATCH 3/4] Simplified falsy comparator test case --- test/collection.js | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/test/collection.js b/test/collection.js index 530a1785..6ee0065f 100644 --- a/test/collection.js +++ b/test/collection.js @@ -603,13 +603,8 @@ $(document).ready(function() { var Col = Backbone.Collection.extend({ comparator: function(model){ return model.id; } }); - var col = new Col([{id: 2}, {id: 1}], { - comparator: false - }); - var otherCol = new Col([{id: 2}, {id: 1}]); - raises(function(){ col.sort() }); - equal(col.first().id, 2); - equal(otherCol.first().id, 1); + var col = new Col(null, {comparator: false}); + ok(!col.comparator); }); }); From c016a1c58f41e9a31f60ac5627befe406294cb6c Mon Sep 17 00:00:00 2001 From: Casey Foster Date: Thu, 24 May 2012 09:11:29 -0700 Subject: [PATCH 4/4] Added a few more cases for the test --- test/collection.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/test/collection.js b/test/collection.js index 6ee0065f..cb6fd655 100644 --- a/test/collection.js +++ b/test/collection.js @@ -603,8 +603,14 @@ $(document).ready(function() { var Col = Backbone.Collection.extend({ comparator: function(model){ return model.id; } }); - var col = new Col(null, {comparator: false}); - ok(!col.comparator); + var col = new Col + var colFalse = new Col(null, {comparator: false}); + var colNull = new Col(null, {comparator: null}); + var colUndefined = new Col(null, {comparator: undefined}); + ok(col.comparator); + ok(!colFalse.comparator); + ok(!colNull.comparator); + ok(colUndefined.comparator); }); });