Skip to content

Ember.SortableMixin: Add support of custom sortFunction - #1216

Closed
sly7-7 wants to merge 8 commits into
emberjs:masterfrom
sly7-7:sortable-custom-sort
Closed

sly7-7 wants to merge 8 commits into
emberjs:masterfrom
sly7-7:sortable-custom-sort

Conversation

@sly7-7

@sly7-7 sly7-7 commented Jul 26, 2012

Copy link
Copy Markdown
Contributor

The default sorting algorithm used in the Routable mixin (based on Ember.compare) was not satisfying in our case.
For example we want 'a' to appear before 'B' (in ascending order)

(new clean PR, I closed #1194

@travisbot

Copy link
Copy Markdown

This pull request passes (merged 22f133a8 into d7e24ac).

@icholy

icholy commented Jul 28, 2012

Copy link
Copy Markdown

+1

@wagenet

wagenet commented Jul 30, 2012

Copy link
Copy Markdown
Member

@tomdale I believe you worked on the SortableMixin some. This seems good to me, but it wouldn't hurt to have some feedback from you.

@sirvine

sirvine commented Aug 5, 2012

Copy link
Copy Markdown

+1 so I can enable proper numeric sort

@wagenet

wagenet commented Oct 8, 2012

Copy link
Copy Markdown
Member

@sly7-7 I think we need to add willChange and didChange callbacks for sortFunction like we have for sortAscending. Other than that, this looks good.

@wagenet

wagenet commented Oct 8, 2012

Copy link
Copy Markdown
Member

Now that I think about it, would it be more appropriate to just override orderBy?

@wagenet

wagenet commented Oct 8, 2012

Copy link
Copy Markdown
Member

@sly7-7 Thanks for the work, but I think the actual answer is to just override orderBy. We might want to document this better. I'd definitely accept a PR for that. Let me know if you disagree.

@wagenet wagenet closed this Oct 8, 2012
@sly7-7

sly7-7 commented Oct 8, 2012

Copy link
Copy Markdown
Contributor Author

Well, let's see what I'm doing for now.

orderBy: function (item1, item2) {
  var 
    result = 0,
    sortProperties = this.get('sortProperties'),
    sortAscending = this.get('sortAscending');

  Ember.assert("you need to define `sortProperties`", !!sortProperties);

  sortProperties.forEach(function (propertyName) {
    if (result === 0) {
      naturalSort.insensitive = true;
      result = naturalSort(Ember.get(item1, propertyName), Ember.get(item2, propertyName));
      if ((result !== 0) && !sortAscending) {
        result = (-1) * result;
      }
    }
  });

  return result;
}

As you can see, it's a stinking copy/paste of the orderBy function, the only difference stays in the naturalSort function... If you provide me a way to refactor this, I would be in favor of only closing this PR. Otherwise, I think it's really a pity to have to do this copy/paste programming, and wonder if there is a really an interrest of this mixin, rather than implementing a custom sort from the Array(Proxy) itself.

@wagenet

wagenet commented Oct 8, 2012

Copy link
Copy Markdown
Member

Ok, I'll reopen for more feedback.

@wagenet wagenet reopened this Oct 8, 2012
@sly7-7

sly7-7 commented Oct 8, 2012

Copy link
Copy Markdown
Contributor Author

Thanks Peter. If there no other feedback here during one day or two, it should mean this is not important, so I will definitively accept to close this :)

@tchak

tchak commented Oct 8, 2012

Copy link
Copy Markdown
Member

+1 for @sly7-7
My first theught was the same as @wagenet, but actually I think override orderBy in most cases is overkill.

@tchak

tchak commented Oct 8, 2012

Copy link
Copy Markdown
Member

maybe we should rename orderBy something different and make orderBy the simple inner method, that should be reimplemented in simple (most) cases.

@lcoq

lcoq commented Oct 9, 2012

Copy link
Copy Markdown
Contributor

+1 for @sly7-7. Overriding orderBy method looks to me in most cases overkill.
And I also dislike sortFunction name, the solution proposed by @tchak looks pretty good to me.

@wagenet

wagenet commented Oct 19, 2012

Copy link
Copy Markdown
Member

@tchak, I like the idea of making orderBy something simple that is intended to be overriden with something lower-level that you can override if you're doing something more complex.

@sly7-7

sly7-7 commented Oct 19, 2012

Copy link
Copy Markdown
Contributor Author

what about leaving orderBy as this, but only rename sortFunction into compare which takes the two parameters to... compare ? The PR is already on my local branch, but 1: I would feedback about it, and 2: currently the build fails due to tests failures about Ember.Instrumentation.

@wagenet

wagenet commented Oct 20, 2012

Copy link
Copy Markdown
Member

@sly7-7 I'm ok with that. @tchak?

@wagenet

wagenet commented Oct 22, 2012

Copy link
Copy Markdown
Member

@sly7-7 Why don't you go ahead and make the change. Not that I'm a big fan of changing APIs but this would be an easy one to deprecate/alias if later we decided something else is more appropriate.

@tchak

tchak commented Oct 22, 2012

Copy link
Copy Markdown
Member

@wagenet @sly7-7 I have one doubt.
compare method is used by Ember.compare internally to compare Ember.Comparable objects.
So if some one will try to compare two ArrayProxies with sortable and comparable mixed in, it will try to compare them with this method (it will obviously not work). I think there could be valid use cases in some app to implement a object level array proxy comparison.

@wagenet

wagenet commented Oct 22, 2012

Copy link
Copy Markdown
Member

@tchak Ah, good point. Maybe we should just leave it as sortFunction.

@sly7-7

sly7-7 commented Oct 22, 2012

Copy link
Copy Markdown
Contributor Author

So, what about comparator ? Perhaps this is even better (as a function name), and not conflicting.

@tchak

tchak commented Oct 22, 2012

Copy link
Copy Markdown
Member

My choice would be _orderBy / orderBy. We already use this paradigm all over the framework for private/public (overridable) functions naming.

@sly7-7

sly7-7 commented Oct 23, 2012

Copy link
Copy Markdown
Contributor Author

I don't like the paradigm of making private functions overridable, but anyway, I won't "chipoter" here :)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of doing a get(this, 'orderBy'), why not just call this.orderBy here? You'll have to make sure this is passed as the second argument to forEach.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Peter, I update this. In fact I'm still quite a JavaScript beginner, so I programmed this through imitation.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well I'm glad you're diving right in despite being a beginner. Thanks for your hard work.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function, like Ember#compare, must return an integer

@ilanah

ilanah commented Nov 12, 2012

Copy link
Copy Markdown

+1 for 'comperator' ...

can you describe the final solution? how should a customized sort method be defined ?

@sly7-7

sly7-7 commented Nov 12, 2012

Copy link
Copy Markdown
Contributor Author

@ilanah You can see an example in the documentation here: https://github.com/sly7-7/ember.js/blob/54010d133252661a4ab6ffce258cca0146992c08/packages/ember-runtime/lib/mixins/sortable.js#L30

If it's not clear for you, just explain me why, and I will try to improve it.

@ilanah

ilanah commented Nov 13, 2012

Copy link
Copy Markdown

10xs, I will check it

2012/11/12 Sylvain MINA [email protected]

@ilanah https://github.com/ilanah You can see an example in the
documentation here:
https://github.com/sly7-7/ember.js/blob/54010d133252661a4ab6ffce258cca0146992c08/packages/ember-runtime/lib/mixins/sortable.js#L30

If it's not clear for you, just explain me why, and I will try to improve
it.

—
Reply to this email directly or view it on GitHubhttps://github.com//pull/1216#issuecomment-10305263.

@sly7-7

sly7-7 commented Nov 30, 2012

Copy link
Copy Markdown
Contributor Author

Close in favor of #1562

@sly7-7 sly7-7 closed this Nov 30, 2012
@oskarols

Copy link
Copy Markdown
Contributor

Since Arrangable was scrapped, could we re-open this?

The docs are still valid, and the use-case for overriding the comparator (not the orderBy-method) is still valid since localeCompare is broken when doing string comparison for many languages (Swedish, German etc).

ef4 pushed a commit that referenced this pull request Jan 6, 2026
NullVoxPopuli-ai-agent pushed a commit to NullVoxPopuli-ai-agent/ember.js that referenced this pull request Sep 29, 2026
Add the `ember-component` deprecation for the default export of
`@ember/component`. It is available in 7.5.0, not enabled, until 8.0.0.

The deprecation fires on `Component.extend()` and in `init`. The `init`
check covers native `class extends Component`.

The rendering test harness renders each test template through a classic
top-level component. It now uses `Component[INTERNAL_COMPONENT_EXTEND]`,
which does not trigger the deprecation.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
NullVoxPopuli-ai-agent pushed a commit to NullVoxPopuli-ai-agent/ember.js that referenced this pull request Oct 1, 2026
Add the `ember-component` deprecation for the default export of
`@ember/component`. It is available in 7.5.0, not enabled, until 8.0.0.

The deprecation fires on `Component.extend()` and in `init`. The `init`
check covers native `class extends Component`.

The rendering test harness renders each test template through a classic
top-level component. It now uses `Component[INTERNAL_COMPONENT_EXTEND]`,
which does not trigger the deprecation.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
NullVoxPopuli-ai-agent pushed a commit to NullVoxPopuli-ai-agent/ember.js that referenced this pull request Oct 1, 2026
Add the `ember-component` deprecation for the default export of
`@ember/component`. It is available in 7.5.0, not enabled, until 8.0.0.

The deprecation fires on `Component.extend()` and in `init`. The `init`
check covers native `class extends Component`.

The rendering test harness renders each test template through a classic
top-level component. It now uses `Component[INTERNAL_COMPONENT_EXTEND]`,
which does not trigger the deprecation.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
NullVoxPopuli-ai-agent pushed a commit to NullVoxPopuli-ai-agent/ember.js that referenced this pull request Oct 1, 2026
Add the `ember-component` deprecation for the default export of
`@ember/component`. It is available in 7.5.0, not enabled, until 8.0.0.

The deprecation fires on `Component.extend()` and in `init`. The `init`
check covers native `class extends Component`.

The rendering test harness renders each test template through a classic
top-level component. It now uses `Component[INTERNAL_COMPONENT_EXTEND]`,
which does not trigger the deprecation.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
NullVoxPopuli-ai-agent pushed a commit to NullVoxPopuli-ai-agent/ember.js that referenced this pull request Oct 1, 2026
Add the `ember-component` deprecation for the default export of
`@ember/component`. It is available in 7.5.0, not enabled, until 8.0.0.

The deprecation fires on `Component.extend()` and in `init`. The `init`
check covers native `class extends Component`.

The rendering test harness renders each test template through a classic
top-level component. It now uses `Component[INTERNAL_COMPONENT_EXTEND]`,
which does not trigger the deprecation.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
NullVoxPopuli-ai-agent pushed a commit to NullVoxPopuli-ai-agent/ember.js that referenced this pull request Oct 31, 2026
Add the `ember-component` deprecation, available in 7.6.0 and not
enabled. It fires in `init`, once for each instance of the default
export of `@ember/component`. Code cannot detect a native
`class extends Component`, so `init` is the one place that sees every
use.

The test harness renders templates through a classic top-level
component, also in the runs where the deprecation counts as removed. It
sets the private `SKIP_DEPRECATION` key on its subclass.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
NullVoxPopuli-ai-agent pushed a commit to NullVoxPopuli-ai-agent/ember.js that referenced this pull request Oct 31, 2026
Add the `ember-component` deprecation, available in 7.6.0 and not
enabled. It fires in `init`, once for each instance of the default
export of `@ember/component`. Code cannot detect a native
`class extends Component`, so `init` is the one place that sees every
use.

The test harness renders templates through a classic top-level
component, also in the runs where the deprecation counts as removed.
A test helper switches the deprecation off while the harness creates
that component, so production code has no exception for it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
NullVoxPopuli-ai-agent pushed a commit to NullVoxPopuli-ai-agent/ember.js that referenced this pull request Oct 31, 2026
Add the `ember-component` deprecation, available in 7.6.0 and not
enabled. It fires in `init`, once for each instance of the default
export of `@ember/component`. Code cannot detect a native
`class extends Component`, so `init` is the one place that sees every
use.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
NullVoxPopuli-ai-agent pushed a commit to NullVoxPopuli-ai-agent/ember.js that referenced this pull request Oct 31, 2026
Add the `ember-component` deprecation, available in 7.6.0 and not
enabled. It fires in `init`, once for each instance of the default
export of `@ember/component`. Code cannot detect a native
`class extends Component`, so `init` is the one place that sees every
use.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
NullVoxPopuli-ai-agent pushed a commit to NullVoxPopuli-ai-agent/ember.js that referenced this pull request Nov 3, 2026
Add the `ember-component` deprecation, available in 7.6.0 and not
enabled. It fires in `init`, once for each instance of the default
export of `@ember/component`. Code cannot detect a native
`class extends Component`, so `init` is the one place that sees every
use.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
NullVoxPopuli-ai-agent pushed a commit to NullVoxPopuli-ai-agent/ember.js that referenced this pull request Nov 6, 2026
Add the `ember-component` deprecation, available in 7.6.0 and not
enabled. It fires in `init`, once for each instance of the default
export of `@ember/component`. Code cannot detect a native
`class extends Component`, so `init` is the one place that sees every
use.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
NullVoxPopuli added a commit that referenced this pull request Nov 11, 2026
…mber-component

Implement RFC #1216: Deprecate the classic Component
lifeart added a commit to lifeart/ember.js that referenced this pull request Nov 13, 2026
GXT branches in shared tests (cache-counter parity, debug render tree,
resolver cache, helper registration through the runtime `hbs`), GXT skips
(hot reload, `{{mount}}`, classic `append`, the rehydration render mode, the
compile-options meta test), and renames that keep the names `array` and
`element` out of module scope in the single-chunk GXT test bundle.

New regression tests, which run on both backends, for the behaviours the
compat backend had wrong: has-block-params in nested blocks; the
(hash)/(array) identity tests (helpers/gxt-hash-array-identity-test.js,
because main rewrote array-test / hash-test as .gjs); attributes are not
arguments and URL attributes forwarded through `...attributes` are
sanitized, also on update (angle-bracket invocation, attribute bindings,
Input, LinkTo); a null/undefined/false attribute or class is removed and a
triple curly in an attribute renders its value (content-test); `<template>`
components invoked by name and classes with a class-body template
(template-tag-by-name-test.gjs, class-body-template-test.gjs,
template-tag-*-test.gjs); a `{{component}}` mustache passed as an argument
is curried; each `{{on}}` and each custom modifier on one element is its own
instance; renderComponent of a custom-managed plain-object definition, and a
root that updates in place, evaluates each binding once and throws only its
own errors. The named-outlets assertion test runs again (its method lacked
`@test`), with the column the compiler reports.

The rebase onto main keeps main's rewritten arranged-content tests, with the
`array` rename on top.

The per-fix history is on lifeart:gxt-pre-squash-b1bf210aef.

Adapted in the rebase onto main 493985e: contextual-components-test.js
is main's file plus this series' new test, and mount-test.js keeps the
GXT skip of the whole file around main's version; the runtime resolver
cache and template factory tests take main's expectations with GXT
branches (no root definition, one `-top-level` hit under the debug
render tree), and main's copies of the old tests in components/classic/
keep the GXT branches the old tests had; angle-bracket-invocation-test.js
imports `ClassicComponent` again for the two tests that use it.

Adapted in the rebase onto main 9ed2915 (RFC emberjs#1216, the classic
Component deprecation): append, attribute-bindings, runtime resolver
cache and template factory tests in components/classic/ take main's
deprecation-guarded files with this series' GXT branches re-applied
unchanged; the new button[formaction]/object[data] sanitization test
calls expectClassicComponentDeprecation() like main's other classic
component tests.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 4.8 <[email protected]>
lifeart added a commit to lifeart/ember.js that referenced this pull request Nov 13, 2026
…r-component deprecation

Main deprecates the classic Component (RFC emberjs#1216, emberjs#21630):
Component#init calls deprecateUntil, so a test that creates a classic
component fails in the "All deprecations enabled" job (unexpected
deprecation) and throws in the "Deprecations as errors" jobs (the
deprecation is removed at 8.0.0). Main guarded its own tests; four tests
this series added were not:

  AngleBracket Invocation (URL sanitization of forwarded attributes):
    a bound URL attribute on a classic component element is sanitized,
    also on update
  AngleBracket Invocation (attributes are not arguments):
    a classic component does not see an attribute as a property
  Components test: argument names:
    a classic component gets an argument named SymbolTable
    a classic component invoked with curlies gets an argument named
    SymbolTable

Each now expects the deprecation and is skipped once it is removed, with
`${testUnless(DEPRECATIONS.DEPRECATE_EMBER_COMPONENT.isRemoved)} <name>`,
the form the deprecations module documents. testUnless(false) is "@test",
so the test names stay the same and native-skip.json and the baselines
still match them.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
NullVoxPopuli added a commit that referenced this pull request Nov 17, 2026
RFC #1216 deprecates the classic `Component` from `@ember/component`.
API docs for APIs that are not deprecated still used it in examples.
They now use `@glimmer/component` in gjs style, or drop the reference.

- runInDebug, bind, next/scheduleOnce, RouterService#recognize:
  examples rewritten to Glimmer components.
- Glimmer Component class docs: no more didInsertElement or this.element.
- ApplicationInstance isBrowser: mention modifiers, not didInsertElement.
- concatenatedProperties and extend: use Controller queryParams,
  remove the Component.extend example.
- Textarea: remove the classic layout/layoutName section.
- associateDestroyableChild and the yield Banner example: import
  Component from @glimmer/component.
- @glimmer/component module intro: say classic components are deprecated.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
lifeart added a commit to lifeart/ember.js that referenced this pull request Nov 20, 2026
GXT branches in shared tests (cache-counter parity, debug render tree,
resolver cache, helper registration through the runtime `hbs`), GXT skips
(hot reload, `{{mount}}`, classic `append`, the rehydration render mode, the
compile-options meta test), and renames that keep the names `array` and
`element` out of module scope in the single-chunk GXT test bundle.

New regression tests, which run on both backends, for the behaviours the
compat backend had wrong: has-block-params in nested blocks; the
(hash)/(array) identity tests (helpers/gxt-hash-array-identity-test.js,
because main rewrote array-test / hash-test as .gjs); attributes are not
arguments and URL attributes forwarded through `...attributes` are
sanitized, also on update (angle-bracket invocation, attribute bindings,
Input, LinkTo); a null/undefined/false attribute or class is removed and a
triple curly in an attribute renders its value (content-test); `<template>`
components invoked by name and classes with a class-body template
(template-tag-by-name-test.gjs, class-body-template-test.gjs,
template-tag-*-test.gjs); a `{{component}}` mustache passed as an argument
is curried; each `{{on}}` and each custom modifier on one element is its own
instance; renderComponent of a custom-managed plain-object definition, and a
root that updates in place, evaluates each binding once and throws only its
own errors. The named-outlets assertion test runs again (its method lacked
`@test`), with the column the compiler reports.

The rebase onto main keeps main's rewritten arranged-content tests, with the
`array` rename on top.

The per-fix history is on lifeart:gxt-pre-squash-b1bf210aef.

Adapted in the rebase onto main 493985e: contextual-components-test.js
is main's file plus this series' new test, and mount-test.js keeps the
GXT skip of the whole file around main's version; the runtime resolver
cache and template factory tests take main's expectations with GXT
branches (no root definition, one `-top-level` hit under the debug
render tree), and main's copies of the old tests in components/classic/
keep the GXT branches the old tests had; angle-bracket-invocation-test.js
imports `ClassicComponent` again for the two tests that use it.

Adapted in the rebase onto main 9ed2915 (RFC emberjs#1216, the classic
Component deprecation): append, attribute-bindings, runtime resolver
cache and template factory tests in components/classic/ take main's
deprecation-guarded files with this series' GXT branches re-applied
unchanged; the new button[formaction]/object[data] sanitization test
calls expectClassicComponentDeprecation() like main's other classic
component tests.

Folded in with the second squash: the tests of the fixes folded into the
compat backend and the native layer: a template's scope, keyword shadowing
and block params named like keywords, dotted paths, in-element
destinations, scope names, each-in forms, `{{#each}}` list paths, argument
names and built-in identity; the classic-dist guard's Node tests
(tests/node/dist-ast-test.cjs); nested and kept `{{#each-in}}` rows; rows
of a list keyed by position (what they yield and invoke, the components
they keep, type changes, each update hook once with the final arguments);
two-way bound arguments of each shape; functions passed to helpers; classic
component classes, curried or with only a layout, in a strict template's
scope. This series' classic component tests expect the ember-component
deprecation.
The history of these commits is on lifeart:gxt-pre-squash2-2063496034.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 4.8 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants