Skip to content

Commit 6a90a5b

Browse files
committed
fix(route-styles): making sure that the route info object has the right shape before continuing fixs #323
1 parent 21c5120 commit 6a90a5b

File tree

8 files changed

+268
-224
lines changed

8 files changed

+268
-224
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ language: node_js
33
node_js:
44
# we recommend testing addons with the same minimum supported node version as Ember CLI
55
# so that your addon works for all apps
6-
- "8"
6+
- "10"
77

88
sudo: false
99
dist: trusty

addon/instance-initializers/route-styles.js

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,37 @@ import initRouteStyles from '../utils/init-route-styles';
33
// This file is removed from the build in Ember < 3.6
44
export function initialize(appInstance) {
55
let router = appInstance.lookup('service:router');
6-
router.on('routeDidChange', function(transition) {
7-
initRouteStyles(appInstance, nestedRouteNames(transition.to));
6+
router.on('routeDidChange', function({ to }) {
7+
if (likeRouteInfo(to)) {
8+
initRouteStyles(appInstance, nestedRouteNames(to));
9+
}
810
});
911

10-
router.on('routeWillChange', function(transition) {
11-
if (transition.to && /_loading$/.test(transition.to.name) && transition.isActive) {
12-
const routeNames = nestedRouteNames(transition.to)
13-
// loading route names are set with an _loading even though
14-
// their path is -loading
15-
.map(name => name.replace(/_loading$/, '-loading'));
16-
initRouteStyles(appInstance, routeNames);
12+
router.on('routeWillChange', function({ to, isActive }) {
13+
if (likeRouteInfo(to)) {
14+
if (/_loading$/.test(to.name) && isActive) {
15+
const routeNames = nestedRouteNames(to)
16+
// loading route names are set with an _loading even though
17+
// their path is -loading
18+
.map(name => name.replace(/_loading$/, '-loading'));
19+
initRouteStyles(appInstance, routeNames);
20+
}
1721
}
1822
});
1923
}
2024

21-
function nestedRouteNames(route = {}, routeNames = []) {
22-
if (route.name) {
23-
routeNames.push(route.name);
24-
}
25-
26-
if (route.parent) {
27-
return nestedRouteNames(route.parent, routeNames);
25+
function nestedRouteNames({ name, parent }, routeNames = []) {
26+
routeNames.push(name);
27+
if (parent) {
28+
return nestedRouteNames(parent, routeNames);
2829
}
2930
return routeNames;
3031
}
3132

33+
function likeRouteInfo(info) {
34+
return info && typeof info === 'object' && info.hasOwnProperty('name');
35+
}
36+
3237
export default {
3338
initialize
3439
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
"ember-cli-htmlbars-inline-precompile": "^2.0.0",
5555
"ember-cli-inject-live-reload": "^2.0.1",
5656
"ember-cli-sri": "^2.1.1",
57-
"ember-cli-styles-preprocessor": "^0.5.3",
57+
"ember-cli-styles-preprocessor": "^0.5.5",
5858
"ember-cli-template-lint": "^1.0.0-beta.1",
5959
"ember-cli-uglify": "^2.1.0",
6060
"ember-disable-prototype-extensions": "^1.1.3",
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { test } from 'qunit';
2+
import moduleForAcceptance from '../../tests/helpers/module-for-acceptance';
3+
4+
moduleForAcceptance('Acceptance | aborted state');
5+
6+
test('when aborting it should finish the transition and not error', function(assert) {
7+
visit('/aborted-state');
8+
9+
10+
andThen(function() {
11+
assert.equal(currentURL(), '/template-style-only');
12+
});
13+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import Route from '@ember/routing/route';
2+
3+
export default Route.extend({
4+
beforeModel(transition) {
5+
transition.abort();
6+
this.transitionTo('template-style-only')
7+
return this._super(...arguments);
8+
},
9+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h1> you will never see me </h1>

tests/dummy/app/router.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ Router.map(function() {
4747
this.route('error-state', function() {
4848
this.route('handled');
4949
});
50+
51+
this.route('aborted-state');
5052
});
5153

5254
export default Router;

0 commit comments

Comments
 (0)