Skip to content

Commit

Permalink
Merge pull request #17041 from emberjs/current-route
Browse files Browse the repository at this point in the history
[FEATURE Router Service] currentRoute
  • Loading branch information
chadhietala authored Oct 4, 2018
2 parents bcc2aff + 8edb93a commit 6a02597
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 4 deletions.
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

0 comments on commit 6a02597

Please sign in to comment.