From ce72c7e482d8217234ab3b73f3049441c71b39b7 Mon Sep 17 00:00:00 2001 From: Trent Willis Date: Thu, 18 Aug 2016 10:07:25 -0700 Subject: [PATCH] [BUGFIX beta] Check that handler exists before triggering event 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. --- packages/ember-routing/lib/system/router.js | 2 +- .../ember-routing/tests/system/router_test.js | 66 ++++++++++++++++++- 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/packages/ember-routing/lib/system/router.js b/packages/ember-routing/lib/system/router.js index 4cd22cb8ac3..5e5b4ab587b 100644 --- a/packages/ember-routing/lib/system/router.js +++ b/packages/ember-routing/lib/system/router.js @@ -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 { diff --git a/packages/ember-routing/tests/system/router_test.js b/packages/ember-routing/tests/system/router_test.js index 736d9aa5990..47443c3af66 100644 --- a/packages/ember-routing/tests/system/router_test.js +++ b/packages/ember-routing/tests/system/router_test.js @@ -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'; @@ -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']); +});