Skip to content

Commit

Permalink
Merge pull request #12749 from rwjblue/router-cleanup
Browse files Browse the repository at this point in the history
Cleanup router & DSL processing.
  • Loading branch information
rwjblue committed Dec 23, 2015
2 parents f367e7a + dac7917 commit 2d622f1
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 29 deletions.
8 changes: 2 additions & 6 deletions packages/ember-application/lib/system/application-instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ const ApplicationInstance = EngineInstance.extend({
*/
startRouting() {
var router = get(this, 'router');
router.startRouting(isResolverModuleBased(this));
router.startRouting();
this._didSetupRouter = true;
},

Expand All @@ -227,7 +227,7 @@ const ApplicationInstance = EngineInstance.extend({
this._didSetupRouter = true;

var router = get(this, 'router');
router.setupRouter(isResolverModuleBased(this));
router.setupRouter();
},

/**
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions packages/ember-routing/lib/system/dsl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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 });
Expand Down
52 changes: 34 additions & 18 deletions packages/ember-routing/lib/system/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,27 +74,21 @@ var EmberRouter = EmberObject.extend(Evented, {
*/
rootURL: '/',

_initRouterJs(moduleBasedResolver) {
_initRouterJs() {
var router = this.router = new Router();
router.triggerEvent = triggerEvent;

router._triggerWillChangeContext = K;
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;
Expand All @@ -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();
Expand All @@ -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.
Expand All @@ -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();
}
Expand All @@ -153,8 +169,8 @@ var EmberRouter = EmberObject.extend(Evented, {
}
},

setupRouter(moduleBasedResolver) {
this._initRouterJs(moduleBasedResolver);
setupRouter() {
this._initRouterJs();
this._setupLocation();

var router = this.router;
Expand Down Expand Up @@ -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) {
Expand Down
7 changes: 5 additions & 2 deletions packages/ember-routing/tests/system/dsl_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 2d622f1

Please sign in to comment.