-
-
Notifications
You must be signed in to change notification settings - Fork 407
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
Ember.computed.sortBy #87
Conversation
This would be nice. The common case is that you specify the ordering explicitly, it's rare that I've wanted to bind the sort properties themselves. |
👍 it's extremely rare that I update the sort property so making the default be simpler is great. I wonder if you could detect if it's a sort definition by the presence of ':' or whether that would be too confusing, so you could use a string instead of an array: newestPosts: Ember.computed.sort('posts', 'createdOn:desc') That might be too easy to footgun yourself by leaving out the sort order and wonder why it doesn't work: newestPosts: Ember.computed.sort('posts', 'createdOn') |
How about introducing Ember.computed.sortBy?
edit: added multiple sort properties example |
@opsb Yea, I had mentioned this in my alternatives, but my |
An alternative would be to implement a new computed macro called: `Ember.computed.sortBy`. This would allow for more argument/API flexibility. Here a few examples: | ||
|
||
```javascript | ||
newestPosts: Ember.computed.sortBy('posts', 'createdOn') # default to ascending |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You've been writing too much Python ;)
=> //
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hahaha. My brain didn't even notice. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ha, it's a ruby week for me...
To me the current alternative looks a little better than the proposal. It lines up nicely with the Ember array methods. This seems like a useful feature and a nice API. In the past people have been adverse to adding more CPMs to Ember, encouraging user-land libraries like https://github.com/cibernox/ember-cpm instead. It will be interesting to hear more feedback. |
@mitchlloyd The more I thought about it, the more that I do like the alternative. But I think we [ember] tend to add lots of little things, and then fold them back together. My inclination was to not increase the surface of the API. But yea, we'll see how interested in this people actually are. If it gets a luke warm response, I'll open a pull to ember-cpm. :) |
```javascript | ||
newestPosts: Ember.computed.sortBy('posts', 'createdOn') # default to ascending | ||
|
||
newestPosts: Ember.computed.sortBy('posts', 'createdOn:desc') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor: this doesn't need naturally, I would expect it to read sortBy fieldNameImSorting
e.g. sortBy firstName. This is the case for array.sortBy and other similar ones like filterBy and mapBy. When we have the arrayPropertyName as the first parameter it breaks the readability. I think that extending the current API makes more sense.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For those of us who lean towards a more functional style when they can, this actually reads pretty normally. For instance with LoDash this would be sortBy(posts, 'createdOn')
. Also see computed.filterBy
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sounds good
I'm in favor of introducing |
I like the idea of a sort by |
After further consideration, I too think Also, FWIW, if there is a green light for |
@workmanw I've noticed that some RFC PRs here change dramatically before they are merged. For instance the approach in the "contextual components" RFC changed so much that the name of the branch (contextual-component-lookup) is at odds with where the proposal landed. Seems like it's nice to keep the history together even if the branch name isn't perfect. |
@mitchlloyd Thanks! I've updated this RFC to reflect |
I am 👍, I'll try to bring this up at the next core team meeting. In the meantime, can you create an addon that provides this capability (which could be used as a polyfill once this feature lands)? |
@rwjblue Sure thing, I'd be happy to get moving on it this weekend. Ultimately, I may have wait for emberjs/ember.js#12221 to be resolved (also see: emberjs/ember.js#12212). But I'll definitely see how far I can get. EDIT: Actually I think because the sort order is immutable, I could make this work without emberjs/ember.js#12221. Stay tuned. |
@rwjblue Done! For those interested in trying it you can use: Edit: This is my first actual public addon. If I messed something up or forgot something, please let me know. |
@workmanw could this just be an update to Or alternatively if you suggest |
This was actually the initial suggestion of this RFC.
This is a pretty good idea that hadn't occurred to me. It also matches the API of |
Nice. My other lingering thought, though I don't know where to go with it, is that microsyntaxes ala
We don't need to avoid the microsyntax, but I encourage you to explore some other ideas before we lock it in. |
I updated the RFC and the polyfill repo to remove the array syntax for the @mixonic I've given some thought to alternatives to the microsyntax, but I haven't come up with anything I loved. Everything just felt too verbose. I'm still open to exploring this, but given that FWIW, the best alternate idea I came up with was:
I had also thought about making a
But I loved this even less. |
I don't mind the tuple style arrays. They are still pretty concise without inventing syntax in a string.
|
I find the microsyntax easier to read and I'd posit it is just as likely to be future-friendly as a tuple-style array. |
To throw my hat in the ring: I prefer the easy to understand but slightly more verbose versions: // option 1
Ember.computed.sortBy('thing', 'propA', {propB: 'desc', propC: 'asc'});
// option 2
Ember.computed.sortBy('thing', 'propA', {property: 'propB', direction: 'desc'}, { property: 'propC', direction: 'asc'}); I like "option 1" the best of these two. I am perfectly happy with supporting the microsyntax also, but would prefer for it to just be sugar on top of a better/easier to grok thing. I find that these various microsyntaxes (that look similar but are different) are quite often a barrier to entry for new folks, and "Option 1" above is actually pretty close to the same number of total characters (slightly more, but not by much)... |
I agree that considering @rwjblue In your option 1, would these have the same behavior? Ember.computed.sortBy('thing', 'propA', {propB: 'desc', propC: 'asc'});
Ember.computed.sortBy('thing', 'propA', {propB: 'desc'}, 'propC');
Ember.computed.sortBy('thing', 'propA', {propB: 'desc'}, {propC: 'asc'}); Or to put it differently; what are the rules for the objects? Are they actually easier to understand? |
I would say that I personally still lean toward the microsyntax because I find it the most concise and readable. Out of the ones proposed I liked this one the best:
It feels like the easiest to read. The two alternates with pojos felt a little more jarring, especially with syntax highlighting. All that said, I don't have a strong opinion about the format at all. I'd be happy with anything that's been suggested here. |
One more variation on the options from @rwjblue. Ember.computed.sortBy('thing', 'propA', { propB: 'desc' }, { propC: 'asc' }); Having the constraint of one object per property might make things more consistent given what @kimroen has shown. "micro:syntax", tuples, objects, no strong preference here either. |
As this is an exercise in ergonomics the microsyntax seems like the best option. It matches the existing syntax for Ember.computed.sort, it just provides a shortcut so that you don't have to use a separate property to specify the sort config. |
@kimroen - It was late and I mistyped (or copied), I meant to take two arguments the array and an object. My example should have been: Ember.computed.sortBy('thing', { propA: 'asc', propB: 'desc', propC: 'asc' }); Also, the ergonomics here (IMHO) should not be about fewest characters typed, but readability/ease of understanding. We can always start with something like I have listed above (which is still much better than having a second property that you have to go track down), and add a super slim string based microsyntax that desugars into the above later... |
I suppose the reason I'm drawn to the microsyntax is that I've already been using it with ember for years, by now it is the most readable syntax. Newcomers may have a different opinion though. I don't see any issue with supporting both syntaxes but I think it would be nice to include the microsyntax for those of us that already familiar with it. |
As I began the bikeshedding, I will now put an end to it :-) The place where the microsyntax is painful is not this proposed As |
@mixonic Alrighty :) I'm happy to continue spending cycles where ever necessary to keep this RFC moving forward. With respect to the microsyntax, it seems like pretty split presently. It may just be one of those things the core team has to call the ball on. |
It's been a few months since there has been any action on this. I have an addon that we've been using for the past few months in production. I currently have the bandwidth to submit an Ember PR if it seems valuable for this to reside in the core. If not, that's fine too, we can close this and keep it as an addon. I'm not sure what the next step here is. The contributing.md is a little vague, but it sounds like I should just submit a PR? It was mentioned bringing this up at a core team meeting for discussion. So I'm not really sure what the next steps are or if the ball is even in my court. |
@workmanw thanks for bringing it up. I will put this on the next core agenda for approval. Is the addon public? Can you link me up? Sorry for the delay, but I will try to get you unblocked on Friday. |
@mixonic Yeap! Here you go: https://github.com/workmanw/ember-computed-sortby |
Not really a fan of Ember.computed.sortBy('thing', { propA: 'asc', propB: 'desc', propC: 'asc' }); as it does not express clearly the fact that the order of the sort properties matters. I don't love the microsyntax API but we should implement it because it is what people expect. If we can decide on a bette API later, we can uniformly deprecate the microsyntax API everywhere that it is used and transition to the new API. |
Ping @mixonic :) I don't mean to keep bugging you, but I'm in a holding pattern right now |
@workmanw core discussions have been waylaid by the holidays :-/ There is strong push-back regarding adding any new CP macros. See also: emberjs/ember.js#12758 I will attempt to raise it again on Friday. Agreed that we should give you a solid direction. |
@mixonic Yea that's what I figured, no worries :)
Oh interesting. I must say that I certainly understand wanting to keep the surface of the API as small as possible and if |
@workmanw! I owe you a direction, for better or worse. We discussed this RFC and the general "CP moratorium" at today's meeting. The consensus is that we want to strictly limit introducing new macros, though there are some cases such as the addition of uniqBy to make our APIs consistent that are justified. Additionally, there is a strong resistance to adding new microsyntax to Ember. These two ideas (no new macros, no new microsyntax) mean we're going to close this RFC without accepting it. We are quite supportive of the ember-computed-sortby addon though. Thanks for the effort on this, and I hope you feel the process made for a better addon if not for an addition to Ember itself. |
Well I can't say I'm not a little disappointed, but I do appreciate it reaching a final conclusion. Thank you. |
Thanks @workmanw @mixonic and everyone involved. I really appreciate the process, even when the outcome might seem negative, it sets a direction and precedent and there's good reasoning behind not doing it. Also, having the addon ecosystem enables innovation outside of core, which I think is perfect for this case. |
I agree with the outcome of this RFC but I am still strongly in favor of filling in this hole in the API. There are three sorting methods and they should all share a uniform API
Presently,
The path forward is thus,
|
Rendered
It's not really a big RFC. Happy to add any further detail, I just didn't want to draw it out when it was straight forward.