Skip to content

Commit 04db211

Browse files
committed
fix(loading-route-styleNamespaces): we needed to hack around and add in some extra hooks to take care of loading routes
1 parent 5abe00b commit 04db211

12 files changed

Lines changed: 89 additions & 24 deletions

File tree

addon/initializers/route-styles.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,17 @@ import initRouteStyles from '../utils/init-route-styles';
66
Router.reopen({
77
didTransition(routes) {
88
this._super(...arguments);
9-
initRouteStyles(getOwner(this), routes);
10-
}
9+
initRouteStyles(getOwner(this), routes.map(route => route.name));
10+
},
11+
12+
intermediateTransitionTo() {
13+
this._super(...arguments);
14+
const routes = this._routerMicrolib.currentHandlerInfos;
15+
const routeNames = routes.map(route => route._handler.routeName.replace(/_loading$/, '-loading'))
16+
17+
initRouteStyles(getOwner(this), routeNames);
18+
},
19+
1120
});
1221

1322
export function initialize() {}

addon/instance-initializers/route-styles.js

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,28 @@ import initRouteStyles from '../utils/init-route-styles';
44
export function initialize(appInstance) {
55
let router = appInstance.lookup('service:router');
66
router.on('routeDidChange', function(transition) {
7-
let routeInfos = [];
8-
let to = transition.to;
9-
10-
while (to) {
11-
routeInfos.push(to);
12-
to = to.parent;
13-
}
14-
15-
routeInfos.reverse();
7+
initRouteStyles(appInstance, nestedRouteNames(transition.to));
8+
});
169

17-
if (routeInfos.length === 0) {
18-
routeInfos = transition.routeInfos;
10+
router.on('routeWillChange', function(transition) {
11+
if (/_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);
1917
}
20-
21-
initRouteStyles(appInstance, routeInfos);
2218
});
2319
}
2420

21+
function nestedRouteNames(route, routeNames = []) {
22+
routeNames.push(route.name);
23+
if (route.parent) {
24+
return nestedRouteNames(route.parent, routeNames);
25+
}
26+
return routeNames;
27+
}
28+
2529
export default {
2630
initialize
2731
};

addon/utils/init-route-styles.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
import podNames from 'ember-component-css/pod-names';
22

3-
export default function initRouteStyles(owner, routes) {
3+
export default function initRouteStyles(owner, routeNames) {
44
const classes = [];
5-
for (let i = 0; i < routes.length; i++) {
6-
let route = routes[i];
7-
let currentPath = route.name.replace(/\./g, '/');
5+
for (let i = 0; i < routeNames.length; i++) {
6+
const routeName = routeNames[i];
7+
const styleNamespace = podNames[routeName.replace(/\./g, '/')];
88

9-
if (podNames[currentPath]) {
10-
owner
11-
.lookup(`controller:${route.name}`)
12-
.set('styleNamespace', podNames[currentPath]);
13-
classes.push(podNames[currentPath]);
9+
if (styleNamespace) {
10+
classes.push(styleNamespace);
11+
12+
const controller = owner.lookup(`controller:${routeName}`);
13+
if (controller) {
14+
controller.set('styleNamespace', styleNamespace);
15+
}
1416
}
1517
}
1618

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { test } from 'qunit';
2+
import moduleForAcceptance from '../../tests/helpers/module-for-acceptance';
3+
import { scheduleOnce } from '@ember/runloop';
4+
5+
moduleForAcceptance(`Acceptance | Unique Paths`);
6+
7+
test('loading state is styled', function(assert) {
8+
visit(`/loading-state/base`);
9+
10+
andThen(function() {
11+
click(find('a'));
12+
assert.equal(find('h1').css('color'), 'rgb(0, 0, 14)');
13+
14+
scheduleOnce('afterRender', function() {
15+
assert.equal(find('h2').css('color'), 'rgb(1, 0, 13)');
16+
});
17+
18+
});
19+
20+
andThen(function() {
21+
assert.equal(find('h3').css('color'), 'rgb(0, 0, 13)');
22+
})
23+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
h1 {
2+
color: rgb(0, 0, 14);
3+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<h1> base </h1>
2+
{{link-to "waiting" "loading-state.waiting"}}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
h2 {
2+
color: rgb(1, 0, 13);
3+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h2> loading </h2>
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+
import { later } from '@ember/runloop';
3+
import RSVP from 'rsvp';
4+
5+
export default Route.extend({
6+
model() {
7+
return new RSVP.Promise(resolve => later(resolve, 500));
8+
}
9+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
h3 {
2+
color: rgb(0, 0, 13);
3+
}

0 commit comments

Comments
 (0)