From 0d6a8e197cf1bf69dcd10b0d12efd8409cfc6533 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Buscht=C3=B6ns?= Date: Fri, 15 Feb 2019 10:30:56 +0100 Subject: [PATCH 1/3] refactor: use router service instead of private API --- README.md | 11 ++++++++--- addon/.gitkeep | 0 addon/helpers/route-task.js | 12 +++++------- addon/utils/route-task.js | 39 ++++++++++++++++++++++++------------- addon/utils/router.js | 31 ----------------------------- app/.gitkeep | 0 6 files changed, 38 insertions(+), 55 deletions(-) delete mode 100644 addon/.gitkeep delete mode 100644 addon/utils/router.js delete mode 100644 app/.gitkeep diff --git a/README.md b/README.md index 426d1a3..97d29a6 100644 --- a/README.md +++ b/README.md @@ -15,13 +15,18 @@ like [**ember-route-action-helper**][ember-route-action-helper] but for ## Installation -This addon will work on Ember versions `1.13.x` and up only, due to use of the -new `Helper` implementation. - ``` ember install ember-route-task-helper ``` +This addon will work on Ember versions `2.4.x` and up only, due to use of the +new `RouterService`. If your Ember version does not natively support it yet, you +need to install the polyfill: + +``` +ember install ember-router-service-polyfill +``` + Of course, you need to have [**ember-concurrency**][ember-concurrency] installed. If you haven't already, run this command first: diff --git a/addon/.gitkeep b/addon/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/addon/helpers/route-task.js b/addon/helpers/route-task.js index 3625d0e..6817040 100644 --- a/addon/helpers/route-task.js +++ b/addon/helpers/route-task.js @@ -1,14 +1,12 @@ import Helper from '@ember/component/helper'; -import { computed, get } from '@ember/object'; -import { getRouter } from 'ember-route-task-helper/utils/router'; -import { routeTaskFromRouter } from 'ember-route-task-helper/utils/route-task'; +import { get } from '@ember/object'; +import { inject as service } from '@ember/service'; +import { routeTaskFromRouterService } from 'ember-route-task-helper/utils/route-task'; export default Helper.extend({ - router: computed(function() { - return getRouter(this); - }).readOnly(), + router: service(), compute([taskName, ...params]) { - return routeTaskFromRouter(get(this, 'router'), taskName, ...params); + return routeTaskFromRouterService(get(this, 'router'), taskName, ...params); } }); diff --git a/addon/utils/route-task.js b/addon/utils/route-task.js index d69110f..552a0a3 100644 --- a/addon/utils/route-task.js +++ b/addon/utils/route-task.js @@ -1,16 +1,9 @@ -import Router from '@ember/routing/router'; +import { getOwner } from '@ember/application'; import { assert } from '@ember/debug'; -import { - getRouter, - findTaskInCurrentRouteHierarchy -} from 'ember-route-task-helper/utils/router'; - -export function routeTaskFromRouter(router, taskName, ...params) { - assert( - '[ember-route-task-helper] No router provided to `routeTaskFromRouter`', - router instanceof Router - ); +import { get } from '@ember/object'; +import { Task } from 'ember-concurrency/-task-property'; +export function routeTaskFromRouterService(router, taskName, ...params) { const task = findTaskInCurrentRouteHierarchy(router, taskName); assert(`[ember-route-task-helper] Unable to find task ${taskName}`, task); @@ -22,8 +15,26 @@ export function routeTaskFromRouter(router, taskName, ...params) { } export default function routeTask(context, taskName, ...params) { - const router = getRouter(context); - assert('[ember-route-task-helper] Unable to lookup router', router); + const router = getOwner(context).lookup('service:router'); + assert('[ember-route-task-helper] Unable to lookup router service', router); + + return routeTaskFromRouterService(router, taskName, ...params); +} + +export function findTaskInCurrentRouteHierarchy(router, taskName) { + const owner = getOwner(router); + let currentRoute = get(router, 'currentRoute'); + + while (currentRoute) { + const realRoute = owner.lookup(`route:${currentRoute.name}`); + const task = get(realRoute, taskName); + + if (task instanceof Task) { + return task; + } + + currentRoute = currentRoute.parent; + } - return routeTaskFromRouter(router, taskName, ...params); + return null; } diff --git a/addon/utils/router.js b/addon/utils/router.js deleted file mode 100644 index cdd63bb..0000000 --- a/addon/utils/router.js +++ /dev/null @@ -1,31 +0,0 @@ -import { A } from '@ember/array'; -import { get } from '@ember/object'; -import { getOwner } from '@ember/application'; -import { Task } from 'ember-concurrency/-task-property'; - -export function getRouter(context) { - return getOwner(context).lookup('router:main'); -} - -export function getCurrentHandlerInfos(router) { - const routerLib = router._routerMicrolib || router.router; - - return routerLib.currentHandlerInfos; -} - -export function getCurrentRoutes(router) { - return A(getCurrentHandlerInfos(router)) - .mapBy('handler') - .reverse(); -} - -export function findTaskInCurrentRouteHierarchy(router, taskName) { - for (const route of getCurrentRoutes(router)) { - const task = get(route, taskName); - if (task instanceof Task) { - return task; - } - } - - return null; -} diff --git a/app/.gitkeep b/app/.gitkeep deleted file mode 100644 index e69de29..0000000 From 162449a3333bd05db4aa32efd011e808b9738fda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Buscht=C3=B6ns?= Date: Fri, 15 Feb 2019 14:13:52 +0100 Subject: [PATCH 2/3] test: add `ember-router-service-polyfill` for old versions --- config/ember-try.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/config/ember-try.js b/config/ember-try.js index fef705d..7d7079f 100644 --- a/config/ember-try.js +++ b/config/ember-try.js @@ -20,7 +20,8 @@ module.exports = function() { npm: { devDependencies: { '@ember/jquery': '^0.5.1', - 'ember-source': '~2.16.0' + 'ember-source': '~2.16.0', + 'ember-router-service-polyfill': '^1.0.3' } } }, @@ -34,7 +35,8 @@ module.exports = function() { npm: { devDependencies: { '@ember/jquery': '^0.5.1', - 'ember-source': '~2.18.0' + 'ember-source': '~2.18.0', + 'ember-router-service-polyfill': '^1.0.3' } } }, From 08bc9b2468de9fbe76988bbbfa9b0fc721276ad8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Buscht=C3=B6ns?= Date: Fri, 15 Feb 2019 15:59:25 +0100 Subject: [PATCH 3/3] refactor: navigate route hierarchy via currentRouteName --- addon/utils/route-task.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/addon/utils/route-task.js b/addon/utils/route-task.js index 552a0a3..9f4f90d 100644 --- a/addon/utils/route-task.js +++ b/addon/utils/route-task.js @@ -23,17 +23,16 @@ export default function routeTask(context, taskName, ...params) { export function findTaskInCurrentRouteHierarchy(router, taskName) { const owner = getOwner(router); - let currentRoute = get(router, 'currentRoute'); + const routeSegments = get(router, 'currentRouteName').split('.'); - while (currentRoute) { - const realRoute = owner.lookup(`route:${currentRoute.name}`); - const task = get(realRoute, taskName); + for (let i = routeSegments.length - 1; i >= 0; i--) { + const routeName = routeSegments.slice(0, i).join('.') || 'application'; + const route = owner.lookup(`route:${routeName}`); + const task = get(route, taskName); if (task instanceof Task) { return task; } - - currentRoute = currentRoute.parent; } return null;