jmorrell / backbone.obscura

A read-only proxy of a Backbone.Collection that can be filtered, sorted, and paginated.
http://jmorrell.github.io/backbone.obscura/
MIT License
107 stars 19 forks source link

Complex setSort() callback #26

Open frank-weindel opened 10 years ago

frank-weindel commented 10 years ago

@jmorrell Great work on this library!

One thing I'd like to do is perform sorts using a compareFunction comparator callback similar to how Array.prototype.sort does it. The callback provides two models as arguments (a and b), and the result of the function is whether A is greater than B, vice-vera, or they're equal. This allows you to do much more complex sorts that are multi-layered and don't have to rely on string comparison.

The following issue calls out this issue as it originally appeared in Backbone. The ability to provide a compareFunction-like sort comparator has since been added.

https://github.com/jashkenas/backbone/issues/488

Would this be difficult to add? I might look into doing it if I get some time soon.

jmorrell commented 10 years ago

Should be possible, and would be a good feature. The relevant code in backbone is here. Note that it switches by testing the number of arguments that the function accepts using Function#length, which we'd want to copy. The code you'd want to modify is in backbone-sorted-collection here.

I'm afraid I don't really have free cycles to work on this at the moment, but I'm happy to review / merge PRs or walk you through code :) Let me know if you want to be added as a collaborator.

frank-weindel commented 9 years ago

Sorry I didn't respond earlier. That would be great Jeremy.

sjmcgrath commented 8 years ago

We just bumped up against this. The relevant similar lines in Backbone.js are https://github.com/jashkenas/backbone/blob/24779ae64a3f325f14d49fe6a8e5aa3e5ac1e421/backbone.js#L1026-L1031.