Skip to content

Commit

Permalink
Add support for routable engines
Browse files Browse the repository at this point in the history
Make {{href-to}} behave the same as {{link-to}} within an engine,
automatically prefixing routes with the engine's namespace

Add {{href-to-external}}, for routes external to the engine.
  • Loading branch information
aaxelb committed May 18, 2018
1 parent 36c2fa8 commit eca5373
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
22 changes: 22 additions & 0 deletions addon/helpers/href-to-external.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import Helper from '@ember/component/helper';
import { getOwner } from '@ember/application';
import { getHrefFromOwner } from './href-to';

function hrefToExternal(context, targetRouteName, ...rest) {
const owner = getOwner(context);

if(owner.mountPoint) {
targetRouteName = owner._getExternalRoute(targetRouteName);
}
return getHrefFromOwner(owner, targetRouteName, ...rest);
}

export default Helper.extend({
compute([targetRouteName, ...rest], namedArgs) {
if(namedArgs.params) {
return hrefToExternal(this, ...namedArgs.params);
} else {
return hrefToExternal(this, targetRouteName, ...rest);
}
}
});
14 changes: 12 additions & 2 deletions addon/helpers/href-to.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,17 @@ import Helper from '@ember/component/helper';
import { getOwner } from '@ember/application';

function hrefTo(context, targetRouteName, ...rest) {
let router = getOwner(context).lookup('service:router');
let owner = getOwner(context);

// If used in an engine, namespace the route
if(owner.mountPoint && targetRouteName !== 'application') {
targetRouteName = `${owner.mountPoint}.${targetRouteName}`;
}
return getHrefFromOwner(owner, targetRouteName, ...rest);
}

function getHrefFromOwner(owner, targetRouteName, ...rest) {
let router = owner.lookup('service:router');

if(router === undefined) {
return;
Expand All @@ -22,7 +32,7 @@ function hrefTo(context, targetRouteName, ...rest) {
return router.urlFor.apply(router, args);
}

export { hrefTo };
export { hrefTo, getHrefFromOwner };

export default Helper.extend({
compute([targetRouteName, ...rest], namedArgs) {
Expand Down

0 comments on commit eca5373

Please sign in to comment.