Skip to content

Commit 41a48de

Browse files
committedFeb 12, 2021
Change the mount method to accept a function for defining engine routes
This change updates the mount API method to accept a function similar to how the route API currently works so that engine routes can be defined in the host application's router.js file. Some Ember applications utilize in-repo ember-engines strictly for their lazy loading and code organization features and not for their isolation features. In this context it makes more sense to define engine routes in the host application's router.js file.
1 parent 7c39e15 commit 41a48de

File tree

2 files changed

+115
-4
lines changed

2 files changed

+115
-4
lines changed
 

‎packages/@ember/-internals/routing/lib/system/dsl.ts

+33-4
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ export interface DSL {
3131
route(name: string, options: RouteOptions, callback: DSLCallback): void;
3232

3333
mount(name: string): void;
34+
mount(name: string, callback: DSLCallback): void;
3435
mount(name: string, options: MountOptions): void;
36+
mount(name: string, options: MountOptions, callback: DSLCallback): void;
3537
}
3638

3739
function isCallback(value?: RouteOptions | DSLCallback): value is DSLCallback {
@@ -173,9 +175,32 @@ export default class DSLImpl implements DSL {
173175
};
174176
}
175177

176-
mount(_name: string, options: MountOptions = {}): void {
177-
let engineRouteMap = this.options.resolveRouteMap(_name);
178+
mount(_name: string): void;
179+
mount(_name: string, mountCallback: DSLCallback): void;
180+
mount(_name: string, options: MountOptions): void;
181+
mount(_name: string, options: MountOptions, mountCallback: DSLCallback): void;
182+
mount(_name: string, _options?: MountOptions | DSLCallback, _mountCallback?: DSLCallback): void {
183+
let engineRouteMap = null;
178184
let name = _name;
185+
let options: MountOptions;
186+
let mountCallback: Option<DSLCallback> = null;
187+
188+
if (isCallback(_options)) {
189+
assert('Unexpected arguments', arguments.length === 2);
190+
options = {};
191+
mountCallback = _options;
192+
} else if (isCallback(_mountCallback)) {
193+
assert('Unexpected arguments', arguments.length === 3);
194+
assert('Unexpected arguments', isOptions(_options));
195+
options = _options;
196+
mountCallback = _mountCallback;
197+
} else {
198+
options = _options || {};
199+
}
200+
201+
if (!mountCallback) {
202+
engineRouteMap = this.options.resolveRouteMap(name);
203+
}
179204

180205
if (options.as) {
181206
name = options.as;
@@ -198,7 +223,7 @@ export default class DSLImpl implements DSL {
198223

199224
let callback;
200225
let dummyErrorRoute = `/_unused_dummy_error_path_route_${name}/:error`;
201-
if (engineRouteMap) {
226+
if (engineRouteMap || mountCallback) {
202227
let shouldResetEngineInfo = false;
203228
let oldEngineInfo = this.options.engineInfo;
204229
if (oldEngineInfo) {
@@ -212,7 +237,11 @@ export default class DSLImpl implements DSL {
212237
createRoute(childDSL, 'loading');
213238
createRoute(childDSL, 'error', { path: dummyErrorRoute });
214239

215-
engineRouteMap.class.call(childDSL);
240+
if (mountCallback) {
241+
mountCallback.call(childDSL);
242+
} else if (engineRouteMap) {
243+
engineRouteMap.class.call(childDSL);
244+
}
216245

217246
callback = childDSL.generate();
218247

‎packages/@ember/-internals/routing/tests/system/dsl_test.js

+82
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,88 @@ moduleFor(
223223
);
224224
}
225225

226+
['@test should allow engine routes to be defined instead of options when mounting an engine'](
227+
assert
228+
) {
229+
assert.expect(4);
230+
231+
Router = Router.map(function () {
232+
this.route('bleep', function () {
233+
this.route('bloop', function () {
234+
this.mount('chat', function chatRoutes() {
235+
this.route('foobar');
236+
});
237+
});
238+
});
239+
});
240+
241+
let engineInstance = buildOwner({
242+
ownerOptions: { routable: true },
243+
});
244+
245+
let router = Router.create();
246+
setOwner(router, engineInstance);
247+
router._initRouterJs();
248+
249+
assert.ok(
250+
router._routerMicrolib.recognizer.names['bleep'],
251+
'parent name was used as base of nested routes'
252+
);
253+
assert.ok(
254+
router._routerMicrolib.recognizer.names['bleep.bloop'],
255+
'parent name was used as base of nested routes'
256+
);
257+
assert.ok(
258+
router._routerMicrolib.recognizer.names['bleep.bloop.chat'],
259+
'parent name was used as base of mounted engine'
260+
);
261+
assert.ok(
262+
router._routerMicrolib.recognizer.names['bleep.bloop.chat.foobar'],
263+
'engine route was passed in from mount'
264+
);
265+
}
266+
267+
['@test should allow engine routes to be defined in addition to options when mounting an engine'](
268+
assert
269+
) {
270+
assert.expect(4);
271+
272+
Router = Router.map(function () {
273+
this.route('bleep', function () {
274+
this.route('bloop', function () {
275+
this.mount('chat', {}, function chatRoutes() {
276+
this.route('foobar');
277+
});
278+
});
279+
});
280+
});
281+
282+
let engineInstance = buildOwner({
283+
ownerOptions: { routable: true },
284+
});
285+
286+
let router = Router.create();
287+
setOwner(router, engineInstance);
288+
router._initRouterJs();
289+
290+
assert.ok(
291+
router._routerMicrolib.recognizer.names['bleep'],
292+
'parent name was used as base of nested routes'
293+
);
294+
assert.ok(
295+
router._routerMicrolib.recognizer.names['bleep.bloop'],
296+
'parent name was used as base of nested routes'
297+
);
298+
assert.ok(
299+
router._routerMicrolib.recognizer.names['bleep.bloop.chat'],
300+
'parent name was used as base of mounted engine'
301+
);
302+
assert.ok(
303+
router._routerMicrolib.recognizer.names['bleep.bloop.chat.foobar'],
304+
'engine route was passed in from mount'
305+
);
306+
}
307+
226308
['@test should allow mounting of engines at a custom path'](assert) {
227309
assert.expect(1);
228310

0 commit comments

Comments
 (0)