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

feat: add fromURL #274

Merged
merged 1 commit into from
Feb 10, 2020
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
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,23 @@ You can also mix and match the parameter styles, however you like.
{{/let}}
```

##### `fromURL`

Instead of the positional & named link parameters described above, you can also
create a `Link` instance from a serialized URL.

```hbs
{{! someURL = "/blogs/tech/posts/dont-break-the-web" }}
{{#let (link fromURL=this.someURL) as |l|}}
<a href={{l.url}} {{on "click" l.transitionTo}}>
Read the next great post.
</a>
{{/let}}
```

`fromURL` is mutually exclusive with the other link parameters: `route`, `model`
& `models`, `query`

### Parameters

In addition to the parameters shown above, the `{{link}}` helper also accepts a
Expand Down Expand Up @@ -280,6 +297,25 @@ Query Params object.
{{/link-to}}
```

##### `@fromURL`

Optional. Mutually exclusive with [`@route`](#route), [`@model`](#model) /
[`@models`](#models), [`@query`](#query).

**Example**

```hbs
<Link @fromURL="/blogs/tech/posts/dont-break-the-web" as |l|>
<a
href={{l.href}}
class={{if l.isActive "is-active"}}
{{on "click" l.transitionTo}}
>
Click me
</a>
</Link>
```

##### `@preventDefault`

Optional. Default: `true`
Expand Down
1 change: 1 addition & 0 deletions addon/components/link/template.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
models=@models
model=@model
query=@query
fromURL=@fromURL
preventDefault=@preventDefault
)
~}}
22 changes: 22 additions & 0 deletions addon/helpers/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ export interface LinkHelperNamedParams
* Optional shortcut for `models={{array model}}`.
*/
model?: RouteModel;

/**
* Instead of any of the other `LinkParams` used to construct a
* `LinkInstance`, you can also provide a serialized URL instead.
*
* This is mutually exclusive with any other `LinkParams`.
*/
fromURL?: string;
}

export default class LinkHelper extends Helper {
Expand All @@ -47,6 +55,20 @@ export default class LinkHelper extends Helper {
!('routeName' in named)
);

if (named.fromURL) {
assert(
`When specifying a serialized 'fromURL' ('${named.fromURL}'), you can't provide any further 'LinkParams'.`,
!([
'route',
'models',
'model',
'query'
] as (keyof LinkHelperNamedParams)[]).some(name => named[name])
);

return this.linkManager.getLinkPramsFromURL(named.fromURL);
}

assert(
`Either pass the target route name as a positional parameter ('${positional[0]}') or pass it as a named parameter ('${named.route}').`,
!(positional[0] && named.route)
Expand Down
30 changes: 29 additions & 1 deletion addon/services/link-manager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { action } from '@ember/object';
import { addListener, removeListener } from '@ember/object/events';
import RouteInfo from '@ember/routing/-private/route-info';
import Transition from '@ember/routing/-private/transition';
import RouterService from '@ember/routing/router-service';
import Service from '@ember/service';
Expand All @@ -8,6 +9,10 @@ import { tracked } from '@glimmer/tracking';

import Link, { LinkParams, UILinkParams, UILink } from '../link';

interface RouterServiceWithRecognize extends RouterService {
recognize(url: string): RouteInfo;
}

export default class LinkManagerService extends Service {
@tracked
private _currentTransitionStack?: Transition[];
Expand All @@ -16,7 +21,7 @@ export default class LinkManagerService extends Service {
* The `RouterService` instance to be used by the generated `Link` instances.
*/
@service('router')
readonly router!: RouterService;
readonly router!: RouterServiceWithRecognize;

/**
* Whether the router has been initialized.
Expand Down Expand Up @@ -50,6 +55,29 @@ export default class LinkManagerService extends Service {
return new UILink(this, { ...linkParams, ...uiParams });
}

/**
* Deserializes the `LinkParams` to be passed to `createLink` / `createUILink`
* from a URL.
*
* If the URL cannot be recognized by the router, an error is thrown.
*/
getLinkPramsFromURL(url: string): LinkParams {
const routeInfo = this.router.recognize(url);
return LinkManagerService.getLinkParamsFromRouteInfo(routeInfo);
}

/**
* Converts a `RouteInfo` object into `LinkParams`.
*/
static getLinkParamsFromRouteInfo(routeInfo: RouteInfo): LinkParams {
const models = routeInfo.paramNames.map(name => routeInfo.params[name]!);
return {
route: routeInfo.name,
query: routeInfo.queryParams,
models
};
}

constructor(properties?: object) {
super(properties);

Expand Down
9 changes: 4 additions & 5 deletions tests/acceptance/link-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,20 +421,19 @@ module('Acceptance | link', function(hooks) {
);
});

test('toString()', async function(this: TestContext, assert) {
test('fromURL', async function(this: TestContext, assert) {
this.Router.map(function() {
this.route('foo');
this.route('bar');
this.route('foo', { path: 'foo/:id' });
});

this.owner.register(
'template:application',
hbs`{{get (link "foo" query=(hash bar=(link "bar"))) "url"}}`
hbs`{{get (link fromURL="/foo/123?bar=qux") "url"}}`
);

await visit('/');
assert.equal(currentURL(), '/');

assert.dom().hasText('/foo?bar=%2Fbar');
assert.dom().hasText('/foo/123?bar=qux');
});
});