diff --git a/addon/services/engine-router-service.js b/addon/services/engine-router-service.js index f91881d..2515f44 100644 --- a/addon/services/engine-router-service.js +++ b/addon/services/engine-router-service.js @@ -6,6 +6,7 @@ import { getOwner } from '@ember/application'; import Evented from '@ember/object/evented'; import { namespaceEngineRouteName } from '../utils/namespace-engine-route-name'; import { getRootOwner } from '../utils/root-owner'; +import { resemblesURL } from '../utils/resembles-url'; export default class EngineRouterService extends Service.extend(Evented) { init() { @@ -61,6 +62,10 @@ export default class EngineRouterService extends Service.extend(Evented) { } transitionTo(routeName, ...args) { + if (resemblesURL(routeName)) { + return get(this, 'externalRouter').transitionTo(routeName); + } + return get(this, 'externalRouter').transitionTo( namespaceEngineRouteName(this._mountPoint, routeName), ...args @@ -75,6 +80,10 @@ export default class EngineRouterService extends Service.extend(Evented) { } replaceWith(routeName, ...args) { + if (resemblesURL(routeName)) { + return get(this, 'externalRouter').replaceWith(routeName); + } + return get(this, 'externalRouter').replaceWith( namespaceEngineRouteName(this._mountPoint, routeName), ...args @@ -115,4 +124,4 @@ export default class EngineRouterService extends Service.extend(Evented) { ...args ); } -} \ No newline at end of file +} diff --git a/addon/utils/resembles-url.js b/addon/utils/resembles-url.js new file mode 100644 index 0000000..de1cbfc --- /dev/null +++ b/addon/utils/resembles-url.js @@ -0,0 +1,11 @@ +/** + * Check if a routeName resembles a url. + * + * @public + * @method namespaceEngineRouteName + * @param {String} str + * @return {boolean} + */ +export function resemblesURL(str) { + return typeof str === 'string' && (str === '' || str[0] === '/'); +} diff --git a/tests/acceptance/routeable-engine-demo-test.js b/tests/acceptance/routeable-engine-demo-test.js index 4dc2858..21115fd 100644 --- a/tests/acceptance/routeable-engine-demo-test.js +++ b/tests/acceptance/routeable-engine-demo-test.js @@ -83,6 +83,23 @@ module('Acceptance | routeless engine demo', function (hooks) { find('.routable-is-active-external-button').classList.contains('is-active-external') ); }); + + test('transitionTo with url transitions to the parent application from within an engine and returns a thenable Transition object', async function ( + assert + ) { + assert.expect(2); + + await visit('/'); + await click('.routeable-engine'); + await click('.blog-post-1-link-ch'); + await click('.routable-transition-to-url-button'); + + assert.equal(currentURL(), '/routable-engine-demo/blog/post/2?lang=Korean'); + + assert.ok( + find('.routable-transition-to-url-button').classList.contains('transitioned-to-url') + ); + }); }); -}); \ No newline at end of file +}); diff --git a/tests/dummy/lib/ember-blog/addon/controllers/post.js b/tests/dummy/lib/ember-blog/addon/controllers/post.js index 33ca46a..7fbc950 100644 --- a/tests/dummy/lib/ember-blog/addon/controllers/post.js +++ b/tests/dummy/lib/ember-blog/addon/controllers/post.js @@ -28,6 +28,12 @@ export default Controller.extend({ if (get(this, 'router').isActiveExternal('home')) { set(this, 'isActiveExternal', true); } + }, + + transitionToUrlByService(url) { + get(this, 'router').transitionTo(url).then(() => { + set(this, 'transitionTo', true); + }); } } -}); \ No newline at end of file +}); diff --git a/tests/dummy/lib/ember-blog/addon/templates/post.hbs b/tests/dummy/lib/ember-blog/addon/templates/post.hbs index 9715b0f..cd50916 100644 --- a/tests/dummy/lib/ember-blog/addon/templates/post.hbs +++ b/tests/dummy/lib/ember-blog/addon/templates/post.hbs @@ -50,3 +50,7 @@ + +