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..9f4f90d 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,25 @@ 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); + const routeSegments = get(router, 'currentRouteName').split('.'); + + 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; + } + } - 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 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' } } },