Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup router & DSL processing. #12749

Merged
merged 1 commit into from
Dec 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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