Skip to content
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

[WIP] [BUGFIX] Make RouterService.transitionTo engine aware #18569

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions packages/@ember/-internals/routing/lib/services/router.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { getOwner } from '@ember/-internals/owner';
import { Evented } from '@ember/-internals/runtime';
import { assert } from '@ember/debug';
import EmberError from '@ember/error';
import { readOnly } from '@ember/object/computed';
import Service from '@ember/service';
import { DEBUG } from '@glimmer/env';
Expand Down Expand Up @@ -120,18 +122,42 @@ export default class RouterService extends Service {
@public
*/
transitionTo(...args: string[]) {
if (resemblesURL(args[0])) {
return this._router._doURLTransition('transitionTo', args[0]);
}

let { routeName, models, queryParams } = extractRouteArgs(args);
routeName = this._prefixRouteName(routeName);

if (resemblesURL(routeName)) {
return this._router._doURLTransition('transitionTo', routeName);
}

let transition = this._router._doTransition(routeName, models, queryParams, true);
transition['_keepDefaultQueryParamValues'] = true;

return transition;
}

/*
Returns a route name which is prefixed based on the mount point

@private
*/
_prefixRouteName(routeName: string) {
let owner = getOwner(this);
let prefix = owner.mountPoint;

// only alter the routeName if it's actually referencing a route.
if (owner.routable && typeof routeName === 'string') {
if (resemblesURL(routeName)) {
throw new EmberError(
'Programmatic transitions by URL cannot be used within an Engine. Please use the route name instead.'
);
} else {
routeName = `${prefix}.${routeName}`;
}
}

return routeName;
}

/**
Transition into another route while replacing the current URL, if possible.
The route may be either a single route or route path:
Expand Down
47 changes: 47 additions & 0 deletions packages/@ember/-internals/routing/tests/services/router-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { buildOwner, moduleFor, AbstractTestCase } from 'internal-test-helpers';
import { setOwner } from '@ember/-internals/owner';
import RouterService from '../../lib/services/router';

moduleFor(
'RouterService with engines',
class extends AbstractTestCase {
["@test transitionTo considers an engine's mountPoint"](assert) {
let router = {
on() {},
off() {},
_doTransition(route) {
return { route };
},
};

let engineInstance = buildOwner({
ownerOptions: {
routable: true,
mountPoint: 'foo.bar',
},
});

let routerService = RouterService.create({ _router: router });
setOwner(routerService, engineInstance);

assert.strictEqual(
routerService.transitionTo('application').route,
'foo.bar.application',
'properly prefixes application route'
);
assert.strictEqual(
routerService.transitionTo('posts').route,
'foo.bar.posts',
'properly prefixes child routes'
);
assert.throws(() => routerService.transitionTo('/posts'), 'throws when trying to use a url');

let queryParams = {};
assert.strictEqual(
routerService.transitionTo(queryParams).route,
queryParams,
'passes query param only transitions through'
);
}
}
);