Skip to content

Commit 433a89f

Browse files
authored
Make transitionTo work with url as an argument (#39)
1 parent 7eadf23 commit 433a89f

File tree

5 files changed

+50
-3
lines changed

5 files changed

+50
-3
lines changed

addon/services/engine-router-service.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { getOwner } from '@ember/application';
66
import Evented from '@ember/object/evented';
77
import { namespaceEngineRouteName } from '../utils/namespace-engine-route-name';
88
import { getRootOwner } from '../utils/root-owner';
9+
import { resemblesURL } from '../utils/resembles-url';
910

1011
export default class EngineRouterService extends Service.extend(Evented) {
1112
init() {
@@ -61,6 +62,10 @@ export default class EngineRouterService extends Service.extend(Evented) {
6162
}
6263

6364
transitionTo(routeName, ...args) {
65+
if (resemblesURL(routeName)) {
66+
return get(this, 'externalRouter').transitionTo(routeName);
67+
}
68+
6469
return get(this, 'externalRouter').transitionTo(
6570
namespaceEngineRouteName(this._mountPoint, routeName),
6671
...args
@@ -75,6 +80,10 @@ export default class EngineRouterService extends Service.extend(Evented) {
7580
}
7681

7782
replaceWith(routeName, ...args) {
83+
if (resemblesURL(routeName)) {
84+
return get(this, 'externalRouter').replaceWith(routeName);
85+
}
86+
7887
return get(this, 'externalRouter').replaceWith(
7988
namespaceEngineRouteName(this._mountPoint, routeName),
8089
...args
@@ -115,4 +124,4 @@ export default class EngineRouterService extends Service.extend(Evented) {
115124
...args
116125
);
117126
}
118-
}
127+
}

addon/utils/resembles-url.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Check if a routeName resembles a url.
3+
*
4+
* @public
5+
* @method namespaceEngineRouteName
6+
* @param {String} str
7+
* @return {boolean}
8+
*/
9+
export function resemblesURL(str) {
10+
return typeof str === 'string' && (str === '' || str[0] === '/');
11+
}

tests/acceptance/routeable-engine-demo-test.js

+18-1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,23 @@ module('Acceptance | routeless engine demo', function (hooks) {
8383
find('.routable-is-active-external-button').classList.contains('is-active-external')
8484
);
8585
});
86+
87+
test('transitionTo with url transitions to the parent application from within an engine and returns a thenable Transition object', async function (
88+
assert
89+
) {
90+
assert.expect(2);
91+
92+
await visit('/');
93+
await click('.routeable-engine');
94+
await click('.blog-post-1-link-ch');
95+
await click('.routable-transition-to-url-button');
96+
97+
assert.equal(currentURL(), '/routable-engine-demo/blog/post/2?lang=Korean');
98+
99+
assert.ok(
100+
find('.routable-transition-to-url-button').classList.contains('transitioned-to-url')
101+
);
102+
});
86103
});
87104

88-
});
105+
});

tests/dummy/lib/ember-blog/addon/controllers/post.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ export default Controller.extend({
2828
if (get(this, 'router').isActiveExternal('home')) {
2929
set(this, 'isActiveExternal', true);
3030
}
31+
},
32+
33+
transitionToUrlByService(url) {
34+
get(this, 'router').transitionTo(url).then(() => {
35+
set(this, 'transitionTo', true);
36+
});
3137
}
3238
}
33-
});
39+
});

tests/dummy/lib/ember-blog/addon/templates/post.hbs

+4
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,7 @@
5050
<button {{action "checkActiveState"}} class="routable-is-active-external-button {{if isActiveExternal 'is-active-external'}}">
5151
isActive in (Engine Router Service)
5252
</button>
53+
54+
<button {{action "transitionToUrlByService" "/routable-engine-demo/blog/post/2?lang=Korean"}} class="routable-transition-to-url-button {{if transitionTo 'transitioned-to-url'}}">
55+
Go to direct URL (Engine Router Service)
56+
</button>

0 commit comments

Comments
 (0)