From dac7917cefad7042cc138fdb5ac07454174df2c6 Mon Sep 17 00:00:00 2001 From: Robert Jackson Date: Wed, 23 Dec 2015 14:04:05 -0500 Subject: [PATCH] Cleanup router & DSL processing. * Remove manual threading of `moduleBasedResolver`. As of the most recent resolver changes we can directly ask the "real" resolver when we need to. This avoids poluting a bunch of places with `moduleBasedResolver` that do not care about that flag. * Prevent creating additional closure just to create application route. * Avoid manually recreating input options for each layer of DSL (just pass options down). * Make DSL creation a separate function. --- .../lib/system/application-instance.js | 8 +-- packages/ember-routing/lib/system/dsl.js | 6 +-- packages/ember-routing/lib/system/router.js | 52 ++++++++++++------- .../ember-routing/tests/system/dsl_test.js | 7 ++- 4 files changed, 44 insertions(+), 29 deletions(-) diff --git a/packages/ember-application/lib/system/application-instance.js b/packages/ember-application/lib/system/application-instance.js index 45ae28331db..2675d455b62 100644 --- a/packages/ember-application/lib/system/application-instance.js +++ b/packages/ember-application/lib/system/application-instance.js @@ -209,7 +209,7 @@ const ApplicationInstance = EngineInstance.extend({ */ startRouting() { var router = get(this, 'router'); - router.startRouting(isResolverModuleBased(this)); + router.startRouting(); this._didSetupRouter = true; }, @@ -227,7 +227,7 @@ const ApplicationInstance = EngineInstance.extend({ this._didSetupRouter = true; var router = get(this, 'router'); - router.setupRouter(isResolverModuleBased(this)); + router.setupRouter(); }, /** @@ -511,10 +511,6 @@ if (isEnabled('ember-application-visit')) { }; } -function isResolverModuleBased(applicationInstance) { - return !!applicationInstance.application.__registry__.resolver.moduleBasedResolver; -} - Object.defineProperty(ApplicationInstance.prototype, 'container', { configurable: true, enumerable: false, diff --git a/packages/ember-routing/lib/system/dsl.js b/packages/ember-routing/lib/system/dsl.js index 1b99e040fab..a7eeaba35eb 100644 --- a/packages/ember-routing/lib/system/dsl.js +++ b/packages/ember-routing/lib/system/dsl.js @@ -10,7 +10,9 @@ function DSL(name, options) { this.enableLoadingSubstates = options && options.enableLoadingSubstates; this.matches = []; this.explicitIndex = undefined; + this.options = options; } + export default DSL; DSL.prototype = { @@ -47,9 +49,7 @@ DSL.prototype = { if (callback) { var fullName = getFullName(this, name, options.resetNamespace); - var dsl = new DSL(fullName, { - enableLoadingSubstates: this.enableLoadingSubstates - }); + var dsl = new DSL(fullName, this.options); createRoute(dsl, 'loading'); createRoute(dsl, 'error', { path: dummyErrorRoute }); diff --git a/packages/ember-routing/lib/system/router.js b/packages/ember-routing/lib/system/router.js index b2a84bc5f14..85b91a470fc 100644 --- a/packages/ember-routing/lib/system/router.js +++ b/packages/ember-routing/lib/system/router.js @@ -74,7 +74,7 @@ var EmberRouter = EmberObject.extend(Evented, { */ rootURL: '/', - _initRouterJs(moduleBasedResolver) { + _initRouterJs() { var router = this.router = new Router(); router.triggerEvent = triggerEvent; @@ -82,19 +82,13 @@ var EmberRouter = EmberObject.extend(Evented, { router._triggerWillLeave = K; var dslCallbacks = this.constructor.dslCallbacks || [K]; - var dsl = new EmberRouterDSL(null, { - enableLoadingSubstates: !!moduleBasedResolver - }); - - function generateDSL() { - this.route('application', { path: '/', resetNamespace: true, overrideNameAssertion: true }, function() { - for (var i = 0; i < dslCallbacks.length; i++) { - dslCallbacks[i].call(this); - } - }); - } + var dsl = this._buildDSL(); - generateDSL.call(dsl); + dsl.route('application', { path: '/', resetNamespace: true, overrideNameAssertion: true }, function() { + for (var i = 0; i < dslCallbacks.length; i++) { + dslCallbacks[i].call(this); + } + }); if (get(this, 'namespace.LOG_TRANSITIONS_INTERNAL')) { router.log = Logger.debug; @@ -103,7 +97,17 @@ var EmberRouter = EmberObject.extend(Evented, { router.map(dsl.generate()); }, + _buildDSL() { + let moduleBasedResolver = this._hasModuleBasedResolver(); + + return new EmberRouterDSL(null, { + enableLoadingSubstates: !!moduleBasedResolver + }); + }, + init() { + this._super(...arguments); + this._activeViews = {}; this._qpCache = new EmptyObject(); this._resetQueuedQueryParameterChanges(); @@ -129,6 +133,18 @@ var EmberRouter = EmberObject.extend(Evented, { return get(this, 'location').getURL(); }), + _hasModuleBasedResolver() { + let owner = getOwner(this); + + if (!owner) { return false; } + + let resolver = owner.application && owner.application.__registry__ && owner.application.__registry__.resolver; + + if (!resolver) { return false; } + + return !!resolver.moduleBasedResolver; + }, + /** Initializes the current router instance and sets up the change handling event listeners used by the instances `location` implementation. @@ -139,10 +155,10 @@ var EmberRouter = EmberObject.extend(Evented, { @method startRouting @private */ - startRouting(moduleBasedResolver) { + startRouting() { var initialURL = get(this, 'initialURL'); - if (this.setupRouter(moduleBasedResolver)) { + if (this.setupRouter()) { if (typeof initialURL === 'undefined') { initialURL = get(this, 'location').getURL(); } @@ -153,8 +169,8 @@ var EmberRouter = EmberObject.extend(Evented, { } }, - setupRouter(moduleBasedResolver) { - this._initRouterJs(moduleBasedResolver); + setupRouter() { + this._initRouterJs(); this._setupLocation(); var router = this.router; @@ -849,7 +865,7 @@ function routeHasBeenDefined(router, name) { (owner.hasRegistration(`template:${name}`) || owner.hasRegistration(`route:${name}`)); } -function triggerEvent(handlerInfos, ignoreFailure, args) { +export function triggerEvent(handlerInfos, ignoreFailure, args) { var name = args.shift(); if (!handlerInfos) { diff --git a/packages/ember-routing/tests/system/dsl_test.js b/packages/ember-routing/tests/system/dsl_test.js index dff80487ad7..b952a6461d2 100644 --- a/packages/ember-routing/tests/system/dsl_test.js +++ b/packages/ember-routing/tests/system/dsl_test.js @@ -117,8 +117,11 @@ QUnit.test('should add loading and error routes if _isRouterMapResult is true', this.route('blork'); }); - var router = Router.create(); - router._initRouterJs(true); + var router = Router.create({ + _hasModuleBasedResolver() { return true; } + }); + + router._initRouterJs(); ok(router.router.recognizer.names['blork'], 'main route was created'); ok(router.router.recognizer.names['blork_loading'], 'loading route was added');