From 2eee7e138b8ffa5a24c1262e04e6881d6262f856 Mon Sep 17 00:00:00 2001 From: Abram Booth Date: Thu, 17 May 2018 16:14:40 -0400 Subject: [PATCH 1/2] Add support for routable engines 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. --- addon/helpers/href-to-external.js | 22 ++++++++++++++++++++++ addon/helpers/href-to.js | 12 +++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 addon/helpers/href-to-external.js diff --git a/addon/helpers/href-to-external.js b/addon/helpers/href-to-external.js new file mode 100644 index 0000000..b5f67ff --- /dev/null +++ b/addon/helpers/href-to-external.js @@ -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); + } + } +}); diff --git a/addon/helpers/href-to.js b/addon/helpers/href-to.js index aa0d354..11aa057 100644 --- a/addon/helpers/href-to.js +++ b/addon/helpers/href-to.js @@ -2,7 +2,17 @@ import Helper from "@ember/component/helper"; import { getOwner } from "@ember/application"; export function hrefTo(context, params) { - let routing = getOwner(context).lookup("service:-routing"); + let owner = getOwner(context); + let ownerIsEngine = Boolean(owner.mountPoint) + + if (ownerIsEngine && targetRouteName !== 'application') { + targetRouteName = `${owner.mountPoint}.${targetRouteName}`; + } + return getHrefFromOwner(owner, params); +} + +export function getHrefFromOwner(owner, params) { + let routing = owner.lookup("service:-routing"); return routing.generateURL(...getParamsForGenerateURL(params)); } From ccd027cd1b81e58ee8332f35a3cc9bd48cfc7630 Mon Sep 17 00:00:00 2001 From: Abram Booth Date: Wed, 13 Jun 2018 08:33:45 -0400 Subject: [PATCH 2/2] Add href-to-external re-export --- app/helpers/href-to-external.js | 1 + 1 file changed, 1 insertion(+) create mode 100644 app/helpers/href-to-external.js diff --git a/app/helpers/href-to-external.js b/app/helpers/href-to-external.js new file mode 100644 index 0000000..9d99b53 --- /dev/null +++ b/app/helpers/href-to-external.js @@ -0,0 +1 @@ +export { default, hrefToExternal } from 'ember-href-to/helpers/href-to-external';