Created
February 29, 2016 01:14
-
-
Save chadhietala/2b3065f3da3d8ae6e2ff to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/packages/ember-routing/lib/system/router.js b/packages/ember-routing/lib/system/router.js | |
index 77764cf..1e239b5 100644 | |
--- a/packages/ember-routing/lib/system/router.js | |
+++ b/packages/ember-routing/lib/system/router.js | |
@@ -12,6 +12,7 @@ import EmberObject from 'ember-runtime/system/object'; | |
import Evented from 'ember-runtime/mixins/evented'; | |
import EmberRouterDSL from 'ember-routing/system/dsl'; | |
import EmberLocation from 'ember-routing/location/api'; | |
+import { asyncInstrument } from 'ember-metal/instrumentation'; | |
import { | |
routeArgs, | |
getActiveTargetName, | |
@@ -22,6 +23,7 @@ import { guidFor } from 'ember-metal/utils'; | |
import RouterState from './router_state'; | |
import { getOwner } from 'container/owner'; | |
import dictionary from 'ember-metal/dictionary'; | |
+import isEnabled from 'ember-metal/features'; | |
/** | |
@module ember | |
@@ -239,6 +241,10 @@ var EmberRouter = EmberObject.extend(Evented, { | |
if (get(this, 'namespace').LOG_TRANSITIONS) { | |
Logger.log(`Transitioned into '${EmberRouter._routePath(infos)}'`); | |
} | |
+ | |
+ if (isEnabled('ember-improved-instrumentation')) { | |
+ this._endInstrumentation(); | |
+ } | |
}, | |
_setOutlets() { | |
@@ -288,6 +294,10 @@ var EmberRouter = EmberObject.extend(Evented, { | |
@since 1.11.0 | |
*/ | |
willTransition(oldInfos, newInfos, transition) { | |
+ if (isEnabled('ember-improved-instrumentation')) { | |
+ this._beginInstrumentation(transition); | |
+ } | |
+ | |
run.once(this, this.trigger, 'willTransition', transition); | |
if (get(this, 'namespace').LOG_TRANSITIONS) { | |
@@ -582,6 +592,8 @@ var EmberRouter = EmberObject.extend(Evented, { | |
emberRouter.didTransition(infos); | |
}; | |
+ // TODO | |
+ //router.willTransition = this.willTransition.bind(this); | |
router.willTransition = function(oldInfos, newInfos, transition) { | |
emberRouter.willTransition(oldInfos, newInfos, transition); | |
}; | |
@@ -721,6 +733,27 @@ var EmberRouter = EmberObject.extend(Evented, { | |
this._slowTransitionTimer = run.scheduleOnce('routerTransitions', this, '_handleSlowTransition', transition, originRoute); | |
}, | |
+ _beginInstrumentation(transition) { | |
+ let callback; | |
+ let intent = transition.intent; | |
+ | |
+ if ('url' in intent) { | |
+ let url = intent.url; | |
+ callback = asyncInstrument('routing.transition.url', () => { return { url }; }); | |
+ } else { | |
+ let name = intent.name; | |
+ callback = () => ({ name }); | |
+ } | |
+ | |
+ this._instrumentationCallback = callback; | |
+ }, | |
+ | |
+ _endInstrumentation() { | |
+ let callback = this._instrumentationCallback; | |
+ callback(); | |
+ this._instrumentationCallback = null; | |
+ }, | |
+ | |
currentState: null, | |
targetState: null, | |
diff --git a/packages/ember/tests/routing/basic_test.js b/packages/ember/tests/routing/basic_test.js | |
index c32e409..096d208 100644 | |
--- a/packages/ember/tests/routing/basic_test.js | |
+++ b/packages/ember/tests/routing/basic_test.js | |
@@ -4077,3 +4077,83 @@ QUnit.test('Exception if outlet name is undefined in render and disconnectOutlet | |
run(function() { router.send('hideModal'); }); | |
}, /You passed undefined as the outlet name/); | |
}); | |
+ | |
+if (isEnabled('ember-improved-instrumentation')) { | |
+ QUnit.test('Should fire routing.transition.url event when the url changes', function() { | |
+ expect(5); | |
+ | |
+ let urls = []; | |
+ | |
+ subscribe('routing.transition.url', { | |
+ before(eventName, ts, path) { | |
+ ok(true, 'instrumentation called for url change'); | |
+ urls.push(path.url); | |
+ }, | |
+ after() {} | |
+ }); | |
+ | |
+ App.Router.map(function() { | |
+ this.route('nork'); | |
+ }); | |
+ | |
+ App.Router.reopen({ | |
+ init() { | |
+ this._super(); | |
+ this.on('willTransition', this.testWillTransitionHook); | |
+ }, | |
+ testWillTransitionHook(transition, url) { | |
+ ok(true, 'transition occured'); | |
+ transition.abort(); | |
+ } | |
+ }); | |
+ | |
+ App.NorkRoute = Route.extend(); | |
+ | |
+ bootApplication(); | |
+ | |
+ run(router, 'handleURL', '/nork'); | |
+ | |
+ run(() => { | |
+ deepEqual(urls, ['/', '/nork']); | |
+ }); | |
+ }); | |
+ | |
+ QUnit.test('Should fire routing.transition.named event when the current route name changes', function() { | |
+ expect(4); | |
+ | |
+ let names = []; | |
+ | |
+ subscribe('routing.transition.named', { | |
+ before(eventName, ts, path) { | |
+ ok(true, 'instrumentation called for url change'); | |
+ names.push(path.name); | |
+ }, | |
+ after() {} | |
+ }); | |
+ | |
+ App.Router.map(function() { | |
+ this.route('nork'); | |
+ }); | |
+ | |
+ App.Router.reopen({ | |
+ init() { | |
+ this._super(); | |
+ this.on('willTransition', this.testWillTransitionHook); | |
+ }, | |
+ testWillTransitionHook(transition, url) { | |
+ ok(true, 'transition occured'); | |
+ transition.abort(); | |
+ } | |
+ }); | |
+ | |
+ App.NorkRoute = Route.extend(); | |
+ | |
+ bootApplication(); | |
+ | |
+ run(router, 'transitionTo', 'nork'); | |
+ | |
+ run(() => { | |
+ deepEqual(names, ['nork']); | |
+ }); | |
+ }); | |
+} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment