Skip to content

Commit

Permalink
Make transitionTo work with url as an argument (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeAstapov committed Jul 12, 2021
1 parent 7eadf23 commit 433a89f
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 3 deletions.
11 changes: 10 additions & 1 deletion addon/services/engine-router-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -115,4 +124,4 @@ export default class EngineRouterService extends Service.extend(Evented) {
...args
);
}
}
}
11 changes: 11 additions & 0 deletions addon/utils/resembles-url.js
Original file line number Diff line number Diff line change
@@ -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] === '/');
}
19 changes: 18 additions & 1 deletion tests/acceptance/routeable-engine-demo-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
);
});
});

});
});
8 changes: 7 additions & 1 deletion tests/dummy/lib/ember-blog/addon/controllers/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
}
});
});
4 changes: 4 additions & 0 deletions tests/dummy/lib/ember-blog/addon/templates/post.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,7 @@
<button {{action "checkActiveState"}} class="routable-is-active-external-button {{if isActiveExternal 'is-active-external'}}">
isActive in (Engine Router Service)
</button>

<button {{action "transitionToUrlByService" "/routable-engine-demo/blog/post/2?lang=Korean"}} class="routable-transition-to-url-button {{if transitionTo 'transitioned-to-url'}}">
Go to direct URL (Engine Router Service)
</button>

0 comments on commit 433a89f

Please sign in to comment.