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

[DEPRECATE] - RFC 421 - deprecate app ctrl router props #17573

Merged
Merged
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
58 changes: 45 additions & 13 deletions packages/@ember/-internals/routing/lib/system/router.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { computed, defineProperty, get, set } from '@ember/-internals/metal';
import { computed, get, notifyPropertyChange, set } from '@ember/-internals/metal';
import { getOwner, Owner } from '@ember/-internals/owner';
import { A as emberA, Evented, Object as EmberObject, typeOf } from '@ember/-internals/runtime';
import { EMBER_ROUTING_ROUTER_SERVICE } from '@ember/canary-features';
import { assert, deprecate, info } from '@ember/debug';
import { HANDLER_INFOS, ROUTER_EVENTS, TRANSITION_STATE } from '@ember/deprecated-features';
import {
APP_CTRL_ROUTER_PROPS,
HANDLER_INFOS,
ROUTER_EVENTS,
TRANSITION_STATE,
} from '@ember/deprecated-features';
import EmberError from '@ember/error';
import { assign } from '@ember/polyfills';
import { cancel, once, run, scheduleOnce } from '@ember/runloop';
Expand Down Expand Up @@ -1635,18 +1640,45 @@ function updatePaths(router: EmberRouter) {
// actually been entered at that point.
return;
}

if (!('currentPath' in appController)) {
defineProperty(appController, 'currentPath');
}

set(appController, 'currentPath', path);

if (!('currentRouteName' in appController)) {
defineProperty(appController, 'currentRouteName');
if (APP_CTRL_ROUTER_PROPS) {
if (!('currentPath' in appController)) {
Object.defineProperty(appController, 'currentPath', {
get() {
deprecate(
'Accessing `currentPath` on `controller:application` is deprecated, use the `currentPath` property on `service:router` instead.',
false,
{
id: 'application-controller.router-properties',
until: '4.0.0',
url:
'https://emberjs.com/deprecations/v3.x#toc_application-controller-router-properties',
}
);
return get(router, 'currentPath');
},
});
}
notifyPropertyChange(appController, 'currentPath');

if (!('currentRouteName' in appController)) {
Object.defineProperty(appController, 'currentRouteName', {
get() {
deprecate(
'Accessing `currentRouteName` on `controller:application` is deprecated, use the `currentRouteName` property on `service:router` instead.',
false,
{
id: 'application-controller.router-properties',
until: '4.0.0',
url:
'https://emberjs.com/deprecations/v3.x#toc_application-controller-router-properties',
}
);
return get(router, 'currentRouteName');
},
});
}
notifyPropertyChange(appController, 'currentRouteName');
}

set(appController, 'currentRouteName', currentRouteName);
}

EmberRouter.reopenClass({
Expand Down
8 changes: 6 additions & 2 deletions packages/@ember/application/tests/reset_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ moduleFor(
let location = initialRouter.get('location');

assert.equal(location.getURL(), '/one');
assert.equal(get(initialApplicationController, 'currentPath'), 'one');
expectDeprecation(() => {
assert.equal(get(initialApplicationController, 'currentPath'), 'one');
}, 'Accessing `currentPath` on `controller:application` is deprecated, use the `currentPath` property on `service:router` instead.');

this.application.reset();

Expand Down Expand Up @@ -153,7 +155,9 @@ moduleFor(
);

assert.equal(location.getURL(), '/one');
assert.equal(get(applicationController, 'currentPath'), 'one');
expectDeprecation(() => {
assert.equal(get(applicationController, 'currentPath'), 'one');
}, 'Accessing `currentPath` on `controller:application` is deprecated, use the `currentPath` property on `service:router` instead.');
});
}

Expand Down
1 change: 1 addition & 0 deletions packages/@ember/deprecated-features/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ export const TRANSITION_STATE = !!'3.9.0';
export const COMPONENT_MANAGER_STRING_LOOKUP = !!'3.8.0';
export const JQUERY_INTEGRATION = !!'3.9.0';
export const ALIAS_METHOD = !!'3.9.0';
export const APP_CTRL_ROUTER_PROPS = !!'4.0.0';
18 changes: 10 additions & 8 deletions packages/ember/tests/helpers/link_to_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1182,14 +1182,16 @@ moduleFor(
return this.visit('/')
.then(() => this.click('#about-link'))
.then(() => {
let currentRouteName = this.applicationInstance
.lookup('controller:application')
.get('currentRouteName');
assert.notEqual(
currentRouteName,
'about',
'link-to should not transition if target is not equal to _self or empty'
);
expectDeprecation(() => {
let currentRouteName = this.applicationInstance
.lookup('controller:application')
.get('currentRouteName');
assert.notEqual(
currentRouteName,
'about',
'link-to should not transition if target is not equal to _self or empty'
);
}, 'Accessing `currentRouteName` on `controller:application` is deprecated, use the `currentRouteName` property on `service:router` instead.');
});
}

Expand Down
74 changes: 48 additions & 26 deletions packages/ember/tests/routing/decoupled_basic_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ moduleFor(
}

get currentPath() {
return this.getController('application').get('currentPath');
let currentPath;
expectDeprecation(() => {
currentPath = this.getController('application').get('currentPath');
}, 'Accessing `currentPath` on `controller:application` is deprecated, use the `currentPath` property on `service:router` instead.');
return currentPath;
}

get currentURL() {
Expand Down Expand Up @@ -1608,15 +1612,12 @@ moduleFor(
1,
'The home template was rendered'
);
assert.equal(
this.applicationInstance.lookup('controller:application').get('currentPath'),
'home'
);
assert.equal(this.currentPath, 'home');
});
}

['@test Redirecting from the middle of a route aborts the remainder of the routes'](assert) {
assert.expect(3);
assert.expect(4);

this.router.map(function() {
this.route('home');
Expand Down Expand Up @@ -1651,18 +1652,21 @@ moduleFor(
return this.visit('/').then(() => {
let router = this.applicationInstance.lookup('router:main');
this.handleURLAborts(assert, '/foo/bar/baz');
assert.equal(
this.applicationInstance.lookup('controller:application').get('currentPath'),
'home'
);
let currentPath;
expectDeprecation(() => {
currentPath = this.applicationInstance
.lookup('controller:application')
.get('currentPath');
}, 'Accessing `currentPath` on `controller:application` is deprecated, use the `currentPath` property on `service:router` instead.');
assert.equal(currentPath, 'home');
assert.equal(router.get('location').getURL(), '/home');
});
}

['@test Redirecting to the current target in the middle of a route does not abort initial routing'](
assert
) {
assert.expect(5);
assert.expect(6);

this.router.map(function() {
this.route('home');
Expand Down Expand Up @@ -1701,18 +1705,21 @@ moduleFor(

return this.visit('/foo/bar/baz').then(() => {
assert.ok(true, '/foo/bar/baz has been handled');
assert.equal(
this.applicationInstance.lookup('controller:application').get('currentPath'),
'foo.bar.baz'
);
let currentPath;
expectDeprecation(() => {
currentPath = this.applicationInstance
.lookup('controller:application')
.get('currentPath');
}, 'Accessing `currentPath` on `controller:application` is deprecated, use the `currentPath` property on `service:router` instead.');
assert.equal(currentPath, 'foo.bar.baz');
assert.equal(successCount, 1, 'transitionTo success handler was called once');
});
}

['@test Redirecting to the current target with a different context aborts the remainder of the routes'](
assert
) {
assert.expect(4);
assert.expect(5);

this.router.map(function() {
this.route('home');
Expand Down Expand Up @@ -1751,10 +1758,13 @@ moduleFor(

return this.visit('/').then(() => {
this.handleURLAborts(assert, '/foo/bar/1/baz');
assert.equal(
this.applicationInstance.lookup('controller:application').get('currentPath'),
'foo.bar.baz'
);
let currentPath;
expectDeprecation(() => {
currentPath = this.applicationInstance
.lookup('controller:application')
.get('currentPath');
}, 'Accessing `currentPath` on `controller:application` is deprecated, use the `currentPath` property on `service:router` instead.');
assert.equal(currentPath, 'foo.bar.baz');
assert.equal(
this.applicationInstance
.lookup('router:main')
Expand Down Expand Up @@ -1792,9 +1802,17 @@ moduleFor(
assert.ok(true, '/foo/bar/baz has been handled');
let applicationController = this.applicationInstance.lookup('controller:application');
let router = this.applicationInstance.lookup('router:main');
assert.equal(applicationController.get('currentPath'), 'foo.bar.baz');

let currentPath;
expectDeprecation(() => {
currentPath = applicationController.get('currentPath');
}, 'Accessing `currentPath` on `controller:application` is deprecated, use the `currentPath` property on `service:router` instead.');
assert.equal(currentPath, 'foo.bar.baz');
run(() => router.send('goToQux'));
assert.equal(applicationController.get('currentPath'), 'foo.qux');
expectDeprecation(() => {
currentPath = applicationController.get('currentPath');
}, 'Accessing `currentPath` on `controller:application` is deprecated, use the `currentPath` property on `service:router` instead.');
assert.equal(currentPath, 'foo.qux');
assert.equal(router.get('location').getURL(), '/foo/qux');
});
}
Expand Down Expand Up @@ -2404,7 +2422,9 @@ moduleFor(
'controller:application',
Controller.extend({
currentPathDidChange: observer('currentPath', function() {
currentPath = this.currentPath;
expectDeprecation(() => {
currentPath = this.currentPath;
}, 'Accessing `currentPath` on `controller:application` is deprecated, use the `currentPath` property on `service:router` instead.');
}),
})
);
Expand Down Expand Up @@ -3094,7 +3114,7 @@ moduleFor(
['@test currentRouteName is a property installed on ApplicationController that can be used in transitionTo'](
assert
) {
assert.expect(24);
assert.expect(36);

this.router.map(function() {
this.route('index', { path: '/' });
Expand All @@ -3117,8 +3137,10 @@ moduleFor(
if (path) {
run(router, 'transitionTo', path);
}
assert.equal(appController.get('currentPath'), expectedPath);
assert.equal(appController.get('currentRouteName'), expectedRouteName);
expectDeprecation(() => {
assert.equal(appController.get('currentPath'), expectedPath);
assert.equal(appController.get('currentRouteName'), expectedRouteName);
}, 'Accessing `currentPath` on `controller:application` is deprecated, use the `currentPath` property on `service:router` instead.');
}

transitionAndCheck(null, 'index', 'index');
Expand Down
13 changes: 10 additions & 3 deletions packages/ember/tests/routing/substates_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ moduleFor(
}

get currentPath() {
return this.getController('application').get('currentPath');
let currentPath;
expectDeprecation(() => {
currentPath = this.getController('application').get('currentPath');
}, 'Accessing `currentPath` on `controller:application` is deprecated, use the `currentPath` property on `service:router` instead.');
return currentPath;
}

['@test Slow promise from a child route of application enters nested loading state'](assert) {
Expand Down Expand Up @@ -178,7 +182,6 @@ moduleFor(

assert.equal(text, 'DUMMY', `dummy template has been rendered`);
});

assert.equal(this.currentPath, 'loading', `loading state entered`);
deferred.resolve();

Expand Down Expand Up @@ -567,7 +570,11 @@ moduleFor(
}

get currentPath() {
return this.getController('application').get('currentPath');
let currentPath;
expectDeprecation(() => {
currentPath = this.getController('application').get('currentPath');
}, 'Accessing `currentPath` on `controller:application` is deprecated, use the `currentPath` property on `service:router` instead.');
return currentPath;
}

['@test ApplicationRoute#currentPath reflects loading state path'](assert) {
Expand Down