From 8edb93afed581b21900636165e5f87c78caf9af7 Mon Sep 17 00:00:00 2001 From: Chad Hietala Date: Thu, 4 Oct 2018 10:56:59 -0400 Subject: [PATCH] [FEATURE Router Service] currentRoute --- .../-internals/routing/lib/services/router.ts | 13 ++++ .../-internals/routing/lib/system/router.ts | 5 ++ .../routing/router_service_test/basic_test.js | 76 ++++++++++++++++++- 3 files changed, 90 insertions(+), 4 deletions(-) diff --git a/packages/@ember/-internals/routing/lib/services/router.ts b/packages/@ember/-internals/routing/lib/services/router.ts index d0467cad2cd..6994fe87ac5 100644 --- a/packages/@ember/-internals/routing/lib/services/router.ts +++ b/packages/@ember/-internals/routing/lib/services/router.ts @@ -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 diff --git a/packages/@ember/-internals/routing/lib/system/router.ts b/packages/@ember/-internals/routing/lib/system/router.ts index 58507fa3c6c..b2d26f6605a 100644 --- a/packages/@ember/-internals/routing/lib/system/router.ts +++ b/packages/@ember/-internals/routing/lib/system/router.ts @@ -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); } } @@ -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(); diff --git a/packages/ember/tests/routing/router_service_test/basic_test.js b/packages/ember/tests/routing/router_service_test/basic_test.js index 1fa50a116ae..9dbf901fe9c 100644 --- a/packages/ember/tests/routing/router_service_test/basic_test.js +++ b/packages/ember/tests/routing/router_service_test/basic_test.js @@ -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'); }); }