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

[FEATURE Router Service] currentRoute #17041

Merged
merged 1 commit into from
Oct 4, 2018
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
13 changes: 13 additions & 0 deletions packages/@ember/-internals/routing/lib/services/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,19 @@ if (EMBER_ROUTING_ROUTER_SERVICE) {
});
},

/**
A RouteInfo that represents the current leaf route.
It is guaranteed to change whenever a route transition
happens (even when that transition only changes parameters
and doesn't change the active route)

@property currentRoute
@type RouteInfo
@category ember-routing-router-service
@public
*/
currentRoute: readOnly('_router.currentRoute'),

/**
Takes a string URL and returns a `RouteInfo` for the leafmost route represented
by the URL. Returns `null` if the URL is not recognized. This method expects to
Expand Down
5 changes: 5 additions & 0 deletions packages/@ember/-internals/routing/lib/system/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ class EmberRouter extends EmberObject {

routeDidChange(transition: Transition) {
if (EMBER_ROUTING_ROUTER_SERVICE) {
router.set('currentRoute', transition.to);
router.trigger('routeDidChange', transition);
}
}
Expand Down Expand Up @@ -489,6 +490,10 @@ class EmberRouter extends EmberObject {
this.currentRouteName = null;
this.currentPath = null;

if (EMBER_ROUTING_ROUTER_SERVICE) {
this.currentRoute = null;
}

this._qpCache = Object.create(null);
this._qpUpdates = new Set();
this._resetQueuedQueryParameterChanges();
Expand Down
76 changes: 72 additions & 4 deletions packages/ember/tests/routing/router_service_test/basic_test.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,121 @@
import { Route, NoneLocation } from '@ember/-internals/routing';
import { set } from '@ember/-internals/metal';
import { RouterTestCase, moduleFor } from 'internal-test-helpers';
import { EMBER_ROUTING_ROUTER_SERVICE } from '@ember/canary-features';

moduleFor(
'Router Service - main',
class extends RouterTestCase {
['@test RouterService#currentRouteName is correctly set for top level route'](assert) {
assert.expect(1);
if (EMBER_ROUTING_ROUTER_SERVICE) {
assert.expect(6);
} else {
assert.expect(1);
}

return this.visit('/').then(() => {
if (EMBER_ROUTING_ROUTER_SERVICE) {
let currentRoute = this.routerService.currentRoute;
let { name, localName, params, paramNames, queryParams } = currentRoute;
assert.equal(name, 'parent.index');
assert.equal(localName, 'index');
assert.deepEqual(params, {});
assert.deepEqual(queryParams, {});
assert.deepEqual(paramNames, []);
}

assert.equal(this.routerService.get('currentRouteName'), 'parent.index');
});
}

['@test RouterService#currentRouteName is correctly set for child route'](assert) {
assert.expect(1);
if (EMBER_ROUTING_ROUTER_SERVICE) {
assert.expect(6);
} else {
assert.expect(1);
}

return this.visit('/child').then(() => {
if (EMBER_ROUTING_ROUTER_SERVICE) {
let currentRoute = this.routerService.currentRoute;
let { name, localName, params, paramNames, queryParams } = currentRoute;
assert.equal(name, 'parent.child');
assert.equal(localName, 'child');
assert.deepEqual(params, {});
assert.deepEqual(queryParams, {});
assert.deepEqual(paramNames, []);
}

assert.equal(this.routerService.get('currentRouteName'), 'parent.child');
});
}

['@test RouterService#currentRouteName is correctly set after transition'](assert) {
assert.expect(1);
if (EMBER_ROUTING_ROUTER_SERVICE) {
assert.expect(5);
} else {
assert.expect(1);
}

return this.visit('/child')
.then(() => {
if (EMBER_ROUTING_ROUTER_SERVICE) {
let currentRoute = this.routerService.currentRoute;
let { name, localName } = currentRoute;
assert.equal(name, 'parent.child');
assert.equal(localName, 'child');
}

return this.routerService.transitionTo('parent.sister');
})
.then(() => {
if (EMBER_ROUTING_ROUTER_SERVICE) {
let currentRoute = this.routerService.currentRoute;
let { name, localName } = currentRoute;
assert.equal(name, 'parent.sister');
assert.equal(localName, 'sister');
}
assert.equal(this.routerService.get('currentRouteName'), 'parent.sister');
});
}

['@test RouterService#currentRouteName is correctly set on each transition'](assert) {
assert.expect(3);
if (EMBER_ROUTING_ROUTER_SERVICE) {
assert.expect(9);
} else {
assert.expect(3);
}

return this.visit('/child')
.then(() => {
if (EMBER_ROUTING_ROUTER_SERVICE) {
let currentRoute = this.routerService.currentRoute;
let { name, localName } = currentRoute;
assert.equal(name, 'parent.child');
assert.equal(localName, 'child');
}
assert.equal(this.routerService.get('currentRouteName'), 'parent.child');

return this.visit('/sister');
})
.then(() => {
if (EMBER_ROUTING_ROUTER_SERVICE) {
let currentRoute = this.routerService.currentRoute;
let { name, localName } = currentRoute;
assert.equal(name, 'parent.sister');
assert.equal(localName, 'sister');
}
assert.equal(this.routerService.get('currentRouteName'), 'parent.sister');

return this.visit('/brother');
})
.then(() => {
if (EMBER_ROUTING_ROUTER_SERVICE) {
let currentRoute = this.routerService.currentRoute;
let { name, localName } = currentRoute;
assert.equal(name, 'parent.brother');
assert.equal(localName, 'brother');
}
assert.equal(this.routerService.get('currentRouteName'), 'parent.brother');
});
}
Expand Down