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

refactor: use router service instead of private API #112

Merged
merged 3 commits into from
Feb 15, 2019
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
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
Empty file removed addon/.gitkeep
Empty file.
12 changes: 5 additions & 7 deletions addon/helpers/route-task.js
Original file line number Diff line number Diff line change
@@ -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);
}
});
38 changes: 24 additions & 14 deletions addon/utils/route-task.js
Original file line number Diff line number Diff line change
@@ -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);

Expand All @@ -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;
}
31 changes: 0 additions & 31 deletions addon/utils/router.js

This file was deleted.

Empty file removed app/.gitkeep
Empty file.
6 changes: 4 additions & 2 deletions config/ember-try.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
}
},
Expand All @@ -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'
}
}
},
Expand Down