From 9bec26ff324bf86385c968852f4b6da55efb4b9a Mon Sep 17 00:00:00 2001 From: Randy Patterson Jr Date: Fri, 23 Oct 2015 10:04:23 -0400 Subject: [PATCH] Update Backbone collection.comparator definition to accpet string and Function types. Closes #6394. --- backbone-associations/backbone-associations-tests.ts | 4 ++-- backbone/backbone-global.d.ts | 6 ++++-- backbone/backbone-tests.ts | 10 ++++++++++ 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/backbone-associations/backbone-associations-tests.ts b/backbone-associations/backbone-associations-tests.ts index 5f656b8f20..92dc9d0bf0 100644 --- a/backbone-associations/backbone-associations-tests.ts +++ b/backbone-associations/backbone-associations-tests.ts @@ -41,7 +41,7 @@ module Backbone.Associations.Tests { } class Locations extends Backbone.Collection { - comparator(c: Backbone.Model) { + comparator = (c: Backbone.Model) => { return c.get("Number"); } } @@ -76,4 +76,4 @@ module Backbone.Associations.Tests { } } -} \ No newline at end of file +} diff --git a/backbone/backbone-global.d.ts b/backbone/backbone-global.d.ts index c16e1a59e3..16c05cf0e5 100644 --- a/backbone/backbone-global.d.ts +++ b/backbone/backbone-global.d.ts @@ -192,8 +192,10 @@ declare module Backbone { fetch(options?: CollectionFetchOptions): JQueryXHR; - comparator(element: TModel): number; - comparator(compare: TModel, to?: TModel): number; + /** + * Specify a model attribute name (string) or function that will be used to sort the collection. + */ + comparator: string | ((element: TModel) => number | string) | ((compare: TModel, to?: TModel) => number); add(model: {}|TModel, options?: AddOptions): TModel; add(models: ({}|TModel)[], options?: AddOptions): TModel[]; diff --git a/backbone/backbone-tests.ts b/backbone/backbone-tests.ts index 4f719f92b0..52d72b5a7f 100644 --- a/backbone/backbone-tests.ts +++ b/backbone/backbone-tests.ts @@ -120,6 +120,16 @@ class Library extends Backbone.Collection { // This model definition is here only to test type compatibility of the model, but it // is not necessary in working code as it is automatically inferred through generics. model: typeof Book; + + constructor(models?: Book[] | Object[], options?: any) { + super(models, options); + + // Test comparator allowed types. + this.comparator = "title"; + this.comparator = (model: Book) => { return 1; }; + this.comparator = (model: Book) => { return "Title"; }; + this.comparator = (model1: Book, model2: Book) => { return 1; }; + } } class Books extends Backbone.Collection { }