Skip to content

Commit

Permalink
[BUGFIX beta] Check that handler exists before triggering event
Browse files Browse the repository at this point in the history
This is necessary for lazily-loaded routes where the handler is not
available synchronously. This includes events like loading and
queryParamsDidChange which trigger synchronously after starting a
transition, in those cases we should by-pass handlers that are not
yet loaded.
  • Loading branch information
Trent Willis authored and trentmwillis committed Aug 19, 2016
1 parent 7a963b0 commit ce72c7e
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/ember-routing/lib/system/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -1059,7 +1059,7 @@ export function triggerEvent(handlerInfos, ignoreFailure, args) {
handlerInfo = handlerInfos[i];
handler = handlerInfo.handler;

if (handler.actions && handler.actions[name]) {
if (handler && handler.actions && handler.actions[name]) {
if (handler.actions[name].apply(handler, args) === true) {
eventWasHandled = true;
} else {
Expand Down
66 changes: 65 additions & 1 deletion packages/ember-routing/tests/system/router_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import HashLocation from 'ember-routing/location/hash_location';
import HistoryLocation from 'ember-routing/location/history_location';
import AutoLocation from 'ember-routing/location/auto_location';
import NoneLocation from 'ember-routing/location/none_location';
import Router from 'ember-routing/system/router';
import Router, { triggerEvent } from 'ember-routing/system/router';
import { runDestroy } from 'ember-runtime/tests/utils';
import buildOwner from 'container/tests/test-helpers/build-owner';
import { setOwner } from 'container/owner';
Expand Down Expand Up @@ -192,3 +192,67 @@ QUnit.test('Router#handleURL should remove any #hashes before doing URL transiti

router.handleURL('/foo/bar?time=morphin#pink-power-ranger');
});

QUnit.test('Router#triggerEvent allows actions to bubble when returning true', function(assert) {
assert.expect(2);

let handlerInfos = [
{
name: 'application',
handler: {
actions: {
loading() {
assert.ok(false, 'loading not handled by application route');
}
}
}
},
{
name: 'about',
handler: {
actions: {
loading() {
assert.ok(true, 'loading handled by about route');
return false;
}
}
}
},
{
name: 'about.me',
handler: {
actions: {
loading() {
assert.ok(true, 'loading handled by about.me route');
return true;
}
}
}
}
];

triggerEvent(handlerInfos, false, ['loading']);
});

QUnit.test('Router#triggerEvent ignores handlers that have not loaded yet', function(assert) {
assert.expect(1);

let handlerInfos = [
{
name: 'about',
handler: {
actions: {
loading() {
assert.ok(true, 'loading handled by about route');
}
}
}
},
{
name: 'about.me',
handler: undefined
}
];

triggerEvent(handlerInfos, false, ['loading']);
});

0 comments on commit ce72c7e

Please sign in to comment.