From 34f50a9e3bab7ed51f8c5726b2a2f26acfdd2a3a Mon Sep 17 00:00:00 2001 From: Artem Gurzhii Date: Thu, 12 Jan 2017 16:12:33 +0200 Subject: [PATCH 1/4] code refactoring for ember-routing package --- .../lib/location/auto_location.js | 6 +-- .../lib/location/history_location.js | 8 +-- .../lib/location/none_location.js | 2 +- packages/ember-routing/lib/location/util.js | 10 ++-- .../ember-routing/lib/services/routing.js | 2 +- packages/ember-routing/lib/system/dsl.js | 8 ++- .../lib/system/generate_controller.js | 2 +- packages/ember-routing/lib/system/route.js | 28 +++++------ packages/ember-routing/lib/system/router.js | 50 +++++++++---------- packages/ember-routing/lib/utils.js | 11 ++-- 10 files changed, 61 insertions(+), 66 deletions(-) diff --git a/packages/ember-routing/lib/location/auto_location.js b/packages/ember-routing/lib/location/auto_location.js index 71507a2cb9d..f97c591e0cc 100644 --- a/packages/ember-routing/lib/location/auto_location.js +++ b/packages/ember-routing/lib/location/auto_location.js @@ -121,7 +121,7 @@ export default EmberObject.extend({ location: this.location, history: this.history, userAgent: this.userAgent, - rootURL: rootURL, + rootURL, documentMode: this.documentMode, global: this.global }); @@ -287,10 +287,10 @@ export function getHashPath(rootURL, location) { if (routePath !== '') { if (routePath.charAt(0) !== '/') { - routePath = '/' + routePath; + routePath = `/${routePath}`; } - path += '#' + routePath; + path += `#${routePath}`; } return path; diff --git a/packages/ember-routing/lib/location/history_location.js b/packages/ember-routing/lib/location/history_location.js index 2f418acd991..10155e1693e 100644 --- a/packages/ember-routing/lib/location/history_location.js +++ b/packages/ember-routing/lib/location/history_location.js @@ -83,8 +83,8 @@ export default EmberObject.extend({ // remove baseURL and rootURL from start of path let url = path - .replace(new RegExp('^' + baseURL + '(?=/|$)'), '') - .replace(new RegExp('^' + rootURL + '(?=/|$)'), ''); + .replace(new RegExp(`^${baseURL}(?=/|$)`), '') + .replace(new RegExp(`^${rootURL}(?=/|$)`), ''); let search = location.search || ''; url += search; @@ -152,7 +152,7 @@ export default EmberObject.extend({ @param path {String} */ pushState(path) { - let state = { path: path }; + let state = { path }; get(this, 'history').pushState(state, null, path); @@ -170,7 +170,7 @@ export default EmberObject.extend({ @param path {String} */ replaceState(path) { - let state = { path: path }; + let state = { path }; get(this, 'history').replaceState(state, null, path); this._historyState = state; diff --git a/packages/ember-routing/lib/location/none_location.js b/packages/ember-routing/lib/location/none_location.js index a45b7305232..60471560765 100644 --- a/packages/ember-routing/lib/location/none_location.js +++ b/packages/ember-routing/lib/location/none_location.js @@ -56,7 +56,7 @@ export default EmberObject.extend({ rootURL = rootURL.replace(/\/$/, ''); // remove rootURL from url - return path.replace(new RegExp('^' + rootURL + '(?=/|$)'), ''); + return path.replace(new RegExp(`^${rootURL}(?=/|$)`), ''); }, /** diff --git a/packages/ember-routing/lib/location/util.js b/packages/ember-routing/lib/location/util.js index ba4d7e7ce47..86edc0b1098 100644 --- a/packages/ember-routing/lib/location/util.js +++ b/packages/ember-routing/lib/location/util.js @@ -4,7 +4,7 @@ Returns the current `location.pathname`, normalized for IE inconsistencies. */ export function getPath(location) { - var pathname = location.pathname; + let pathname = location.pathname; // Various versions of IE/Opera don't always return a leading slash if (pathname.charAt(0) !== '/') { pathname = `/${pathname}`; @@ -33,8 +33,8 @@ export function getQuery(location) { https://bugzilla.mozilla.org/show_bug.cgi?id=483304 */ export function getHash(location) { - var href = location.href; - var hashIndex = href.indexOf('#'); + let href = location.href; + let hashIndex = href.indexOf('#'); if (hashIndex === -1) { return ''; @@ -48,11 +48,11 @@ export function getFullPath(location) { } export function getOrigin(location) { - var origin = location.origin; + let origin = location.origin; // Older browsers, especially IE, don't have origin if (!origin) { - origin = location.protocol + '//' + location.hostname; + origin = `${location.protocol}//${location.hostname}`; if (location.port) { origin += `:${location.port}`; diff --git a/packages/ember-routing/lib/services/routing.js b/packages/ember-routing/lib/services/routing.js index 73b4f4b5f49..fab97e7d370 100644 --- a/packages/ember-routing/lib/services/routing.js +++ b/packages/ember-routing/lib/services/routing.js @@ -66,7 +66,7 @@ export default Service.extend({ this.normalizeQueryParams(routeName, models, visibleQueryParams); let args = routeArgs(routeName, models, visibleQueryParams); - return router.generate.apply(router, args); + return router.generate(...args); }, isActiveForRoute(contexts, queryParams, routeName, routerState, isCurrentWhenSpecified) { diff --git a/packages/ember-routing/lib/system/dsl.js b/packages/ember-routing/lib/system/dsl.js index b62cc843fa2..46471d8ca53 100644 --- a/packages/ember-routing/lib/system/dsl.js +++ b/packages/ember-routing/lib/system/dsl.js @@ -121,9 +121,7 @@ function getFullName(dsl, name, resetNamespace) { } } -function createRoute(dsl, name, options, callback) { - options = options || {}; - +function createRoute(dsl, name, options = {}, callback) { let fullName = getFullName(dsl, name, options.resetNamespace); if (typeof options.path !== 'string') { @@ -133,7 +131,7 @@ function createRoute(dsl, name, options, callback) { dsl.push(options.path, fullName, callback, options.serialize); } -DSL.map = function(callback) { +DSL.map = callback => { let dsl = new DSL(); callback.call(dsl); return dsl; @@ -150,7 +148,7 @@ DSL.prototype.mount = function(_name, _options) { name = options.as; } - var fullName = getFullName(this, name, options.resetNamespace); + let fullName = getFullName(this, name, options.resetNamespace); let engineInfo = { name: _name, diff --git a/packages/ember-routing/lib/system/generate_controller.js b/packages/ember-routing/lib/system/generate_controller.js index a82609ae9c1..e61b79d1738 100644 --- a/packages/ember-routing/lib/system/generate_controller.js +++ b/packages/ember-routing/lib/system/generate_controller.js @@ -49,7 +49,7 @@ export default function generateController(owner, controllerName) { let instance = owner.lookup(fullName); if (get(instance, 'namespace.LOG_ACTIVE_GENERATION')) { - info(`generated -> ${fullName}`, { fullName: fullName }); + info(`generated -> ${fullName}`, { fullName }); } return instance; diff --git a/packages/ember-routing/lib/system/route.js b/packages/ember-routing/lib/system/route.js index 3dae41323ba..0793acc5365 100644 --- a/packages/ember-routing/lib/system/route.js +++ b/packages/ember-routing/lib/system/route.js @@ -231,19 +231,19 @@ let Route = EmberObject.extend(ActionHandler, Evented, { let scopedPropertyName = `${controllerName}:${propName}`; let qp = { undecoratedDefaultValue: get(controllerProto, propName), - defaultValue: defaultValue, + defaultValue, serializedDefaultValue: defaultValueSerialized, serializedValue: defaultValueSerialized, - type: type, - urlKey: urlKey, + type, + urlKey, prop: propName, - scopedPropertyName: scopedPropertyName, - controllerName: controllerName, + scopedPropertyName, + controllerName, route: this, - parts: parts, // provided later when stashNames is called if 'model' scope + parts, // provided later when stashNames is called if 'model' scope values: null, // provided later when setup is called. no idea why. - scope: scope + scope }; map[propName] = map[urlKey] = map[scopedPropertyName] = qp; @@ -252,9 +252,9 @@ let Route = EmberObject.extend(ActionHandler, Evented, { } return { - qps: qps, - map: map, - propertyNames: propertyNames, + qps, + map, + propertyNames, states: { /* Called when a query parameter changes in the URL, this route cares @@ -908,7 +908,7 @@ let Route = EmberObject.extend(ActionHandler, Evented, { transition.method('replace'); } - qpMeta.qps.forEach(function(qp) { + qpMeta.qps.forEach(qp => { let routeQpMeta = get(qp.route, '_qp'); let finalizedController = qp.route.controller; finalizedController._qpDelegate = get(routeQpMeta, 'states.active'); @@ -1330,7 +1330,7 @@ let Route = EmberObject.extend(ActionHandler, Evented, { let allParams = queryParams.propertyNames; let cache = this._bucketCache; - allParams.forEach(function(prop) { + allParams.forEach(prop => { let aQp = queryParams.map[prop]; aQp.values = params; @@ -2399,8 +2399,8 @@ function mergeEachQueryParams(controllerQP, routeQP) { } function addQueryParamsObservers(controller, propNames) { - propNames.forEach(function(prop) { - controller.addObserver(prop + '.[]', controller, controller._qpChanged); + propNames.forEach(prop => { + controller.addObserver(`${prop}.[]`, controller, controller._qpChanged); }); } diff --git a/packages/ember-routing/lib/system/router.js b/packages/ember-routing/lib/system/router.js index fd79b318928..2a9af3075c8 100644 --- a/packages/ember-routing/lib/system/router.js +++ b/packages/ember-routing/lib/system/router.js @@ -122,11 +122,9 @@ const EmberRouter = EmberObject.extend(Evented, { let owner = getOwner(this); let router = this; - options.resolveRouteMap = function(name) { - return owner[FACTORY_FOR]('route-map:' + name); - }; + options.resolveRouteMap = name => owner[FACTORY_FOR](`route-map:${name}`); - options.addRouteForEngine = function(name, engineInfo) { + options.addRouteForEngine = (name, engineInfo) => { if (!router._engineInfoByRoute[name]) { router._engineInfoByRoute[name] = engineInfo; } @@ -216,7 +214,7 @@ const EmberRouter = EmberObject.extend(Evented, { this._setupRouter(router, location); - location.onUpdateURL((url) => { + location.onUpdateURL(url => { this.handleURL(url); }); @@ -576,7 +574,7 @@ const EmberRouter = EmberObject.extend(Evented, { routeName = engineInfo.localFullName; } - let fullRouteName = 'route:' + routeName; + let fullRouteName = `route:${routeName}`; let handler = routeOwner.lookup(fullRouteName); @@ -627,31 +625,31 @@ const EmberRouter = EmberObject.extend(Evented, { router.getHandler = this._getHandlerFunction(); router.getSerializer = this._getSerializerFunction(); - let doUpdateURL = function() { + let doUpdateURL = () => { location.setURL(lastURL); }; - router.updateURL = function(path) { + router.updateURL = (path) => { lastURL = path; run.once(doUpdateURL); }; if (location.replaceURL) { - let doReplaceURL = function() { + let doReplaceURL = () => { location.replaceURL(lastURL); }; - router.replaceURL = function(path) { + router.replaceURL = path => { lastURL = path; run.once(doReplaceURL); }; } - router.didTransition = function(infos) { + router.didTransition = infos => { emberRouter.didTransition(infos); }; - router.willTransition = function(oldInfos, newInfos, transition) { + router.willTransition = (oldInfos, newInfos, transition) => { emberRouter.willTransition(oldInfos, newInfos, transition); }; }, @@ -766,7 +764,7 @@ const EmberRouter = EmberObject.extend(Evented, { this._prepareQueryParams(targetRouteName, models, queryParams); let transitionArgs = routeArgs(targetRouteName, models, queryParams); - let transition = this.router.transitionTo.apply(this.router, transitionArgs); + let transition = this.router.transitionTo(...transitionArgs); didBeginTransition(transition, this); @@ -1019,7 +1017,7 @@ const EmberRouter = EmberObject.extend(Evented, { let owner = getOwner(this); assert( - 'You attempted to mount the engine \'' + name + '\' in your router map, but the engine can not be found.', + `You attempted to mount the engine '${name}' in your router map, but the engine can not be found.`, owner.hasRegistration(`engine:${name}`) ); @@ -1086,7 +1084,7 @@ let defaultActionHandlers = { let handlerInfos = transition.state.handlerInfos; let router = originRoute.router; - forEachRouteAbove(originRoute, handlerInfos, function(route) { + forEachRouteAbove(originRoute, handlerInfos, route => { // Check for the existence of an 'error' route. // We don't check for an 'error' route on the originRoute, since that would // technically be below where we're at in the route hierarchy. @@ -1108,7 +1106,7 @@ let defaultActionHandlers = { return true; }); - logError(error, 'Error while processing route: ' + transition.targetName); + logError(error, `Error while processing route: ${transition.targetName}`); }, // Attempt to find an appropriate loading route or substate to enter. @@ -1116,7 +1114,7 @@ let defaultActionHandlers = { let handlerInfos = transition.state.handlerInfos; let router = originRoute.router; - forEachRouteAbove(originRoute, handlerInfos, function(route) { + forEachRouteAbove(originRoute, handlerInfos, route => { // Check for the existence of a 'loading' route. // We don't check for a 'loading' route on the originRoute, since that would // technically be below where we're at in the route hierarchy. @@ -1176,10 +1174,10 @@ function findRouteSubstateName(route, state) { let owner = getOwner(route); let routeName = route.routeName; - let substateName = routeName + '_' + state; + let substateName = `${routeName}_${state}`; let routeNameFull = route.fullRouteName; - let substateNameFull = routeNameFull + '_' + state; + let substateNameFull = `${routeNameFull}_${state}`; return routeHasBeenDefined(owner, router, substateName, substateNameFull) ? substateNameFull : @@ -1201,10 +1199,10 @@ function findRouteStateName(route, state) { let owner = getOwner(route); let routeName = route.routeName; - let stateName = routeName === 'application' ? state : routeName + '.' + state; + let stateName = routeName === 'application' ? state : `${routeName}.${state}`; let routeNameFull = route.fullRouteName; - let stateNameFull = routeNameFull === 'application' ? state : routeNameFull + '.' + state; + let stateNameFull = routeNameFull === 'application' ? state : `${routeNameFull}.${state}`; return routeHasBeenDefined(owner, router, stateName, stateNameFull) ? stateNameFull : @@ -1402,7 +1400,7 @@ EmberRouter.reopenClass({ oldNameParts.shift(); } - path.push.apply(path, nameParts.slice(oldNameParts.length)); + path.push(...nameParts.slice(oldNameParts.length)); } return path.join('.'); @@ -1421,7 +1419,7 @@ function didBeginTransition(transition, router) { } router.set('targetState', routerState); - transition.promise = transition.catch(function(error) { + transition.promise = transition.catch(error => { let errorId = guidFor(error); if (router._isErrorHandled(errorId)) { @@ -1501,7 +1499,7 @@ function appendLiveRoute(liveRoutes, defaultParentState, renderOptions) { } } return { - liveRoutes: liveRoutes, + liveRoutes, ownState: myState }; } @@ -1516,9 +1514,9 @@ function appendOrphan(liveRoutes, into, myState) { }; } liveRoutes.outlets.__ember_orphans__.outlets[into] = myState; - run.schedule('afterRender', function() { + run.schedule('afterRender', () => { // `wasUsed` gets set by the render helper. - assert('You attempted to render into \'' + into + '\' but it was not found', + assert(`You attempted to render into '${into}' but it was not found`, liveRoutes.outlets.__ember_orphans__.outlets[into].wasUsed); }); } diff --git a/packages/ember-routing/lib/utils.js b/packages/ember-routing/lib/utils.js index 0876d962e4b..259568fe05c 100644 --- a/packages/ember-routing/lib/utils.js +++ b/packages/ember-routing/lib/utils.js @@ -7,10 +7,10 @@ const ALL_PERIODS_REGEX = /\./g; export function routeArgs(targetRouteName, models, queryParams) { let args = []; if (typeof targetRouteName === 'string') { - args.push('' + targetRouteName); + args.push(`${targetRouteName}`); } - args.push.apply(args, models); - args.push({ queryParams: queryParams }); + args.push(...models); + args.push({ queryParams }); return args; } @@ -76,8 +76,7 @@ function _calculateCacheValuePrefix(prefix, part) { /* Stolen from Controller */ -export function calculateCacheKey(prefix, _parts, values) { - let parts = _parts || []; +export function calculateCacheKey(prefix, parts = [], values) { let suffixes = ''; for (let i = 0; i < parts.length; ++i) { let part = parts[i]; @@ -91,7 +90,7 @@ export function calculateCacheKey(prefix, _parts, values) { value = get(values, part); } } - suffixes += '::' + part + ':' + value; + suffixes += `::${part}:${value}`; } return prefix + suffixes.replace(ALL_PERIODS_REGEX, '-'); } From 6bb52c3af4ebdaf08222cb15157b697d772fbcbd Mon Sep 17 00:00:00 2001 From: Artem Gurzhii Date: Thu, 12 Jan 2017 17:17:52 +0200 Subject: [PATCH 2/4] dsl mount default argument --- packages/ember-routing/lib/system/dsl.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/ember-routing/lib/system/dsl.js b/packages/ember-routing/lib/system/dsl.js index 46471d8ca53..45ce9a16f19 100644 --- a/packages/ember-routing/lib/system/dsl.js +++ b/packages/ember-routing/lib/system/dsl.js @@ -139,8 +139,7 @@ DSL.map = callback => { let uuid = 0; -DSL.prototype.mount = function(_name, _options) { - let options = _options || {}; +DSL.prototype.mount = function(_name, options = {}) { let engineRouteMap = this.options.resolveRouteMap(_name); let name = _name; From cb54a5bf85d959bf5cfdf73f03fed8fac03024fa Mon Sep 17 00:00:00 2001 From: Artem Gurzhii Date: Thu, 12 Jan 2017 17:34:49 +0200 Subject: [PATCH 3/4] replace prototypal on classical inheritance --- packages/ember-routing/lib/system/dsl.js | 180 +++++++++++------------ 1 file changed, 90 insertions(+), 90 deletions(-) diff --git a/packages/ember-routing/lib/system/dsl.js b/packages/ember-routing/lib/system/dsl.js index 45ce9a16f19..eba86a63345 100644 --- a/packages/ember-routing/lib/system/dsl.js +++ b/packages/ember-routing/lib/system/dsl.js @@ -6,17 +6,17 @@ import { assert, deprecate } from 'ember-metal'; @submodule ember-routing */ -function DSL(name, options) { - this.parent = name; - this.enableLoadingSubstates = options && options.enableLoadingSubstates; - this.matches = []; - this.explicitIndex = undefined; - this.options = options; -} +let uuid = 0; -export default DSL; +class DSL { + constructor(name, options) { + this.parent = name; + this.enableLoadingSubstates = options && options.enableLoadingSubstates; + this.matches = []; + this.explicitIndex = undefined; + this.options = options; + } -DSL.prototype = { route(name, options, callback) { let dummyErrorRoute = `/_unused_dummy_error_path_route_${name}/:error`; if (arguments.length === 2 && typeof options === 'function') { @@ -30,11 +30,11 @@ DSL.prototype = { assert( `'${name}' cannot be used as a route name.`, - (function() { + ((() => { if (options.overrideNameAssertion === true) { return true; } return ['array', 'basic', 'object', 'application'].indexOf(name) === -1; - })() + }))() ); if (this.enableLoadingSubstates) { @@ -55,7 +55,7 @@ DSL.prototype = { } else { createRoute(this, name, options); } - }, + } push(url, name, callback, serialize) { let parts = name.split('.'); @@ -76,7 +76,7 @@ DSL.prototype = { if (url === '' || url === '/' || parts[parts.length - 1] === 'index') { this.explicitIndex = true; } this.matches.push([url, name, callback]); - }, + } resource(name, options, callback) { if (arguments.length === 2 && typeof options === 'function') { @@ -91,7 +91,7 @@ DSL.prototype = { options.resetNamespace = true; deprecate('this.resource() is deprecated. Use this.route(\'name\', { resetNamespace: true }, function () {}) instead.', false, { id: 'ember-routing.router-resource', until: '3.0.0' }); this.route(name, options, callback); - }, + } generate() { let dslMatches = this.matches; @@ -107,7 +107,82 @@ DSL.prototype = { } }; } -}; + + mount(_name, options = {}) { + let engineRouteMap = this.options.resolveRouteMap(_name); + let name = _name; + + if (options.as) { + name = options.as; + } + + let fullName = getFullName(this, name, options.resetNamespace); + + let engineInfo = { + name: _name, + instanceId: uuid++, + mountPoint: fullName, + fullName + }; + + let path = options.path; + + if (typeof path !== 'string') { + path = `/${name}`; + } + + let callback; + let dummyErrorRoute = `/_unused_dummy_error_path_route_${name}/:error`; + if (engineRouteMap) { + let shouldResetEngineInfo = false; + let oldEngineInfo = this.options.engineInfo; + if (oldEngineInfo) { + shouldResetEngineInfo = true; + this.options.engineInfo = engineInfo; + } + + let optionsForChild = assign({ engineInfo }, this.options); + let childDSL = new DSL(fullName, optionsForChild); + + createRoute(childDSL, 'loading'); + createRoute(childDSL, 'error', { path: dummyErrorRoute }); + + + engineRouteMap.class.call(childDSL); + + callback = childDSL.generate(); + + if (shouldResetEngineInfo) { + this.options.engineInfo = oldEngineInfo; + } + } + + let localFullName = 'application'; + let routeInfo = assign({ localFullName }, engineInfo); + + if (this.enableLoadingSubstates) { + // These values are important to register the loading routes under their + // proper names for the Router and within the Engine's registry. + let substateName = `${name}_loading`; + let localFullName = `application_loading`; + let routeInfo = assign({ localFullName }, engineInfo); + createRoute(this, substateName, { resetNamespace: options.resetNamespace }); + this.options.addRouteForEngine(substateName, routeInfo); + + substateName = `${name}_error`; + localFullName = `application_error`; + routeInfo = assign({ localFullName }, engineInfo); + createRoute(this, substateName, { resetNamespace: options.resetNamespace, path: dummyErrorRoute }); + this.options.addRouteForEngine(substateName, routeInfo); + } + + this.options.addRouteForEngine(fullName, routeInfo); + + this.push(path, fullName, callback); + } +} + +export default DSL; function canNest(dsl) { return dsl.parent && dsl.parent !== 'application'; @@ -136,78 +211,3 @@ DSL.map = callback => { callback.call(dsl); return dsl; }; - -let uuid = 0; - -DSL.prototype.mount = function(_name, options = {}) { - let engineRouteMap = this.options.resolveRouteMap(_name); - let name = _name; - - if (options.as) { - name = options.as; - } - - let fullName = getFullName(this, name, options.resetNamespace); - - let engineInfo = { - name: _name, - instanceId: uuid++, - mountPoint: fullName, - fullName - }; - - let path = options.path; - - if (typeof path !== 'string') { - path = `/${name}`; - } - - let callback; - let dummyErrorRoute = `/_unused_dummy_error_path_route_${name}/:error`; - if (engineRouteMap) { - let shouldResetEngineInfo = false; - let oldEngineInfo = this.options.engineInfo; - if (oldEngineInfo) { - shouldResetEngineInfo = true; - this.options.engineInfo = engineInfo; - } - - let optionsForChild = assign({ engineInfo }, this.options); - let childDSL = new DSL(fullName, optionsForChild); - - createRoute(childDSL, 'loading'); - createRoute(childDSL, 'error', { path: dummyErrorRoute }); - - - engineRouteMap.class.call(childDSL); - - callback = childDSL.generate(); - - if (shouldResetEngineInfo) { - this.options.engineInfo = oldEngineInfo; - } - } - - let localFullName = 'application'; - let routeInfo = assign({ localFullName }, engineInfo); - - if (this.enableLoadingSubstates) { - // These values are important to register the loading routes under their - // proper names for the Router and within the Engine's registry. - let substateName = `${name}_loading`; - let localFullName = `application_loading`; - let routeInfo = assign({ localFullName }, engineInfo); - createRoute(this, substateName, { resetNamespace: options.resetNamespace }); - this.options.addRouteForEngine(substateName, routeInfo); - - substateName = `${name}_error`; - localFullName = `application_error`; - routeInfo = assign({ localFullName }, engineInfo); - createRoute(this, substateName, { resetNamespace: options.resetNamespace, path: dummyErrorRoute }); - this.options.addRouteForEngine(substateName, routeInfo); - } - - this.options.addRouteForEngine(fullName, routeInfo); - - this.push(path, fullName, callback); -}; From 456fb420d242627c4e34aa17e7a0139b355627f0 Mon Sep 17 00:00:00 2001 From: Artem Gurzhii Date: Sun, 15 Jan 2017 02:20:23 +0200 Subject: [PATCH 4/4] minor changes to ember-routing package --- packages/ember-routing/lib/ext/controller.js | 4 ++-- .../lib/location/auto_location.js | 8 +++---- .../lib/location/hash_location.js | 2 +- .../lib/location/history_location.js | 10 ++++---- packages/ember-routing/lib/location/util.js | 2 +- .../ember-routing/lib/services/routing.js | 2 +- packages/ember-routing/lib/system/dsl.js | 23 +++++-------------- packages/ember-routing/lib/system/route.js | 18 ++++++--------- packages/ember-routing/lib/system/router.js | 8 +++---- 9 files changed, 32 insertions(+), 45 deletions(-) diff --git a/packages/ember-routing/lib/ext/controller.js b/packages/ember-routing/lib/ext/controller.js index 2511017ee3a..86a2d4b0065 100644 --- a/packages/ember-routing/lib/ext/controller.js +++ b/packages/ember-routing/lib/ext/controller.js @@ -55,8 +55,8 @@ ControllerMixin.reopen({ @method _qpChanged @private */ - _qpChanged(controller, _prop) { - let prop = _prop.substr(0, _prop.length - 3); + _qpChanged(controller, prop) { + prop = prop.substr(0, prop.length - 3); let delegate = controller._qpDelegate; let value = get(controller, prop); diff --git a/packages/ember-routing/lib/location/auto_location.js b/packages/ember-routing/lib/location/auto_location.js index f97c591e0cc..437b300ccbb 100644 --- a/packages/ember-routing/lib/location/auto_location.js +++ b/packages/ember-routing/lib/location/auto_location.js @@ -255,18 +255,18 @@ export function getHistoryPath(rootURL, location) { // If the path already has a trailing slash, remove the one // from the hashed route so we don't double up. - if (path.slice(-1) === '/') { + if (path.charAt(path.length - 1) === '/') { routeHash = routeHash.substr(1); } // This is the "expected" final order - path = path + routeHash + query; + path += routeHash + query; if (hashParts.length) { path += `#${hashParts.join('#')}`; } } else { - path = path + query + hash; + path += query + hash; } return path; @@ -286,7 +286,7 @@ export function getHashPath(rootURL, location) { let routePath = historyPath.substr(rootURL.length); if (routePath !== '') { - if (routePath.charAt(0) !== '/') { + if (routePath[0] !== '/') { routePath = `/${routePath}`; } diff --git a/packages/ember-routing/lib/location/hash_location.js b/packages/ember-routing/lib/location/hash_location.js index 629fc9b60b2..d027128d86d 100644 --- a/packages/ember-routing/lib/location/hash_location.js +++ b/packages/ember-routing/lib/location/hash_location.js @@ -56,7 +56,7 @@ export default EmberObject.extend({ let originalPath = this.getHash().substr(1); let outPath = originalPath; - if (outPath.charAt(0) !== '/') { + if (outPath[0] !== '/') { outPath = '/'; // Only add the # if the path isn't empty. diff --git a/packages/ember-routing/lib/location/history_location.js b/packages/ember-routing/lib/location/history_location.js index 10155e1693e..2d477606ea3 100644 --- a/packages/ember-routing/lib/location/history_location.js +++ b/packages/ember-routing/lib/location/history_location.js @@ -29,7 +29,10 @@ export default EmberObject.extend({ this._super(...arguments); let base = document.querySelector('base'); - let baseURL = base ? base.getAttribute('href') : ''; + let baseURL = ''; + if (base) { + baseURL = base.getAttribute('href'); + } set(this, 'baseURL', baseURL); set(this, 'location', get(this, 'location') || window.location); @@ -87,8 +90,7 @@ export default EmberObject.extend({ .replace(new RegExp(`^${rootURL}(?=/|$)`), ''); let search = location.search || ''; - url += search; - url += this.getHash(); + url += search + this.getHash(); return url; }, @@ -218,7 +220,7 @@ export default EmberObject.extend({ // remove trailing slashes if they exists rootURL = rootURL.replace(/\/$/, ''); baseURL = baseURL.replace(/\/$/, ''); - } else if (baseURL.match(/^\//) && rootURL.match(/^\//)) { + } else if (baseURL[0] === '/' && rootURL[0] === '/') { // if baseURL and rootURL both start with a slash // ... remove trailing slash from baseURL if it exists baseURL = baseURL.replace(/\/$/, ''); diff --git a/packages/ember-routing/lib/location/util.js b/packages/ember-routing/lib/location/util.js index 86edc0b1098..90a7df36cf7 100644 --- a/packages/ember-routing/lib/location/util.js +++ b/packages/ember-routing/lib/location/util.js @@ -6,7 +6,7 @@ export function getPath(location) { let pathname = location.pathname; // Various versions of IE/Opera don't always return a leading slash - if (pathname.charAt(0) !== '/') { + if (pathname[0] !== '/') { pathname = `/${pathname}`; } diff --git a/packages/ember-routing/lib/services/routing.js b/packages/ember-routing/lib/services/routing.js index fab97e7d370..9ed54191d3b 100644 --- a/packages/ember-routing/lib/services/routing.js +++ b/packages/ember-routing/lib/services/routing.js @@ -97,7 +97,7 @@ export default Service.extend({ function numberOfContextsAcceptedByHandler(handler, handlerInfos) { let req = 0; for (let i = 0; i < handlerInfos.length; i++) { - req = req + handlerInfos[i].names.length; + req += handlerInfos[i].names.length; if (handlerInfos[i].handler === handler) { break; } diff --git a/packages/ember-routing/lib/system/dsl.js b/packages/ember-routing/lib/system/dsl.js index eba86a63345..8a76cdc3335 100644 --- a/packages/ember-routing/lib/system/dsl.js +++ b/packages/ember-routing/lib/system/dsl.js @@ -17,25 +17,18 @@ class DSL { this.options = options; } - route(name, options, callback) { + route(name, options = {}, callback) { let dummyErrorRoute = `/_unused_dummy_error_path_route_${name}/:error`; if (arguments.length === 2 && typeof options === 'function') { callback = options; options = {}; } - if (arguments.length === 1) { - options = {}; - } - - assert( - `'${name}' cannot be used as a route name.`, - ((() => { - if (options.overrideNameAssertion === true) { return true; } + assert(`'${name}' cannot be used as a route name.`, (() => { + if (options.overrideNameAssertion === true) { return true; } - return ['array', 'basic', 'object', 'application'].indexOf(name) === -1; - }))() - ); + return ['array', 'basic', 'object', 'application'].indexOf(name) === -1; + })()); if (this.enableLoadingSubstates) { createRoute(this, `${name}_loading`, { resetNamespace: options.resetNamespace }); @@ -78,16 +71,12 @@ class DSL { this.matches.push([url, name, callback]); } - resource(name, options, callback) { + resource(name, options = {}, callback) { if (arguments.length === 2 && typeof options === 'function') { callback = options; options = {}; } - if (arguments.length === 1) { - options = {}; - } - options.resetNamespace = true; deprecate('this.resource() is deprecated. Use this.route(\'name\', { resetNamespace: true }, function () {}) instead.', false, { id: 'ember-routing.router-resource', until: '3.0.0' }); this.route(name, options, callback); diff --git a/packages/ember-routing/lib/system/route.js b/packages/ember-routing/lib/system/route.js index 0793acc5365..a0f5b245af7 100644 --- a/packages/ember-routing/lib/system/route.js +++ b/packages/ember-routing/lib/system/route.js @@ -38,8 +38,7 @@ const { slice } = Array.prototype; function K() { return this; } export function defaultSerialize(model, params) { - if (params.length < 1) { return; } - if (!model) { return; } + if (params.length < 1 || !model) { return; } let name = params[0]; let object = {}; @@ -300,8 +299,7 @@ let Route = EmberObject.extend(ActionHandler, Evented, { @method _stashNames */ - _stashNames(_handlerInfo, dynamicParent) { - let handlerInfo = _handlerInfo; + _stashNames(handlerInfo, dynamicParent) { if (this._names) { return; } let names = this._names = handlerInfo._names; @@ -1582,9 +1580,7 @@ let Route = EmberObject.extend(ActionHandler, Evented, { } else if (!name) { if (transition.resolveIndex < 1) { return; } - let parentModel = transition.state.handlerInfos[transition.resolveIndex - 1].context; - - return parentModel; + return transition.state.handlerInfos[transition.resolveIndex - 1].context; } return this.findModel(name, value); @@ -2173,8 +2169,9 @@ let Route = EmberObject.extend(ActionHandler, Evented, { // backward compatibility with our existing semantics, which allow // any route to disconnectOutlet things originally rendered by any // other route. This should all get cut in 2.0. - this.router.router. - currentHandlerInfos[i].handler._disconnectOutlet(outletName, parentView); + this.router.router + .currentHandlerInfos[i] + .handler._disconnectOutlet(outletName, parentView); } }, @@ -2234,10 +2231,9 @@ function parentRoute(route) { return handlerInfo && handlerInfo.handler; } -function handlerInfoFor(route, handlerInfos, _offset) { +function handlerInfoFor(route, handlerInfos, offset = 0) { if (!handlerInfos) { return; } - let offset = _offset || 0; let current; for (let i = 0; i < handlerInfos.length; i++) { current = handlerInfos[i].handler; diff --git a/packages/ember-routing/lib/system/router.js b/packages/ember-routing/lib/system/router.js index 2a9af3075c8..27fa082ed9a 100644 --- a/packages/ember-routing/lib/system/router.js +++ b/packages/ember-routing/lib/system/router.js @@ -562,7 +562,7 @@ const EmberRouter = EmberObject.extend(Evented, { let seen = new EmptyObject(); let owner = getOwner(this); - return (name) => { + return name => { let routeName = name; let routeOwner = owner; let engineInfo = this._engineInfoByRoute[routeName]; @@ -606,7 +606,7 @@ const EmberRouter = EmberObject.extend(Evented, { }, _getSerializerFunction() { - return (name) => { + return name => { let engineInfo = this._engineInfoByRoute[name]; // If this is not an Engine route, we fall back to the handler for serialization @@ -629,7 +629,7 @@ const EmberRouter = EmberObject.extend(Evented, { location.setURL(lastURL); }; - router.updateURL = (path) => { + router.updateURL = path => { lastURL = path; run.once(doUpdateURL); }; @@ -1431,7 +1431,7 @@ function didBeginTransition(transition, router) { } function resemblesURL(str) { - return typeof str === 'string' && (str === '' || str.charAt(0) === '/'); + return typeof str === 'string' && (str === '' || str[0] === '/'); } function forEachQueryParam(router, handlerInfos, queryParams, callback) {