New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Collection to receive falsy `comparator` to override default #1342

Merged
merged 5 commits into from May 24, 2012

Conversation

Projects
None yet
4 participants
@caseywebdev
Collaborator

caseywebdev commented May 23, 2012

The alternative to this is something like

new Collection models, comparator: ->

but that seems a little dirty to me as I don't want to sort it differently or with a 'fake' sort function, but rather not sort it all like a vanilla Backbone.Collection would do. This allows me to pass in

new Collection models, comparator: false # or null or '' or 0

to return the collection to 'no sort' mode. I came across this while implementing a search. Every other time I want this collection to sort by a name.

class Collection extends Backbone.Collection
  comparator: (model) -> model.get 'name'

But, the order of items initialized in the collection from the search is specific to the query function. When I do

new Collection searchResults, comparator: false

I can keep my order.

@wookiehangover

This comment has been minimized.

Collaborator

wookiehangover commented May 24, 2012

+1 being able to opt-out of sorting instead of passing a noop seems like a valid use case to support. @caseywebdev I submitted a pull req to you that adds a test which fails without your patch.

@braddunbar

This comment has been minimized.

Collaborator

braddunbar commented May 24, 2012

+1 - Seems like a reasonable option to me, though a bit backwards incompatible.

@braddunbar

This comment has been minimized.

Collaborator

braddunbar commented May 24, 2012

Looks good. Thanks @caseywebdev and @wookiehangover!

braddunbar added a commit that referenced this pull request May 24, 2012

Merge pull request #1342 from caseywebdev/master
Allow Collection to receive falsy `comparator` to override default

@braddunbar braddunbar merged commit b6dc6a2 into jashkenas:master May 24, 2012

@jashkenas

This comment has been minimized.

Owner

jashkenas commented May 24, 2012

How should we best document this "feature"?

@braddunbar

This comment has been minimized.

Collaborator

braddunbar commented May 24, 2012

When creating a Collection, you may choose to pass in the initial array of models. The collection's comparator function may be included as an option. If you define an initialize function, it will be invoked when the collection is created.

I was thinking I'd add the to above "Passing false as the comparator option will prevent sorting."

How's that sound?

@jashkenas

This comment has been minimized.

Owner

jashkenas commented May 24, 2012

Perfecto.

braddunbar added a commit that referenced this pull request May 24, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment