Skip to content

Commit

Permalink
[BUGFIX stable] Allow store to be injected on Route
Browse files Browse the repository at this point in the history
In the public types, TypeScript (correctly) complains when users try to
inject the store with `@service store` on a `Route` subclass, because
the item is an accessor on `Route` itself, and the service injection
"happens" to work only because the `set store(value)` smashes itself as
well as the corresponding `get store()` using `defineProperty`.

Work around this by renaming the getter in question to `_store` and
updating the one usage to check for a `store` property before falling
back to the `_store` property. Happily, this also dovetails with the
plan to deprecate the automatic `findModel` behavior this exists to
support: once that is removed, this entire code path can be cleaned up
trivially.
  • Loading branch information
chriskrycho committed Jun 28, 2023
1 parent 1107974 commit e1c62b5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
14 changes: 8 additions & 6 deletions packages/@ember/routing/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1234,7 +1234,13 @@ class Route<Model = unknown> extends EmberObject.extend(ActionHandler, Evented)
@private
*/
findModel(...args: any[]) {
return (get(this, 'store') as any).find(...args);
// SAFETY: it's all absurd lies; there is no explicit contract for `store`
// and we allow people to register *anything* here and we call it: GLHF! The
// fallback path here means we correctly handle the case where there is no
// explicit store injection on the route subclass.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let store = ('store' in this ? this.store : get(this, '_store')) as any;
return store.find(...args);
}

/**
Expand Down Expand Up @@ -1573,7 +1579,7 @@ class Route<Model = unknown> extends EmberObject.extend(ActionHandler, Evented)
@private
*/
@computed
protected get store() {
protected get _store() {
const owner = getOwner(this);
assert('Route is unexpectedly missing an owner', owner);
let routeName = this.routeName;
Expand Down Expand Up @@ -1620,10 +1626,6 @@ class Route<Model = unknown> extends EmberObject.extend(ActionHandler, Evented)
};
}

protected set store(value: any) {
defineProperty(this, 'store', null, value);
}

/**
@private
@property _qp
Expand Down
2 changes: 1 addition & 1 deletion tests/docs/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ module.exports = {
'sort',
'sortBy',
'startRouting',
'store',
'_store',
'subscribe',
'sum',
'tagName',
Expand Down

0 comments on commit e1c62b5

Please sign in to comment.