Ember.js V5 - Deprecate Router Events

This automation removes all calls to willTransition or didTransition events on the Router via usage of routeWillChange event listener and routeDidChange event listener.

Ember.js
jscodeshift
Estimated time saving
5 minutes/occurrence
Change mode
Autonomous
Applicability criteria

Ember.js version higher or equal to 3.6.

Made by
Rajasegar Chandran
Rajasegar Chandran

Usage →

Codemod CLI:

intalling vs code extension tooltip icon
codemod ember/5/deprecate-router-events
copy CLI command icon

Codemod VS Code extension:

intalling vs code extension tooltip icon
vs code logo
Run in VS Code

Use/edit codemod in Codemod Studio:

intuita logo without text
Open in Codemod Studio

Description

This codemod removes all calls to willTransition or didTransition events on the Router via usage of routeWillChange event listener and routeDidChange event listener.

Example

Before:

import Router from '@ember/routing/router';
import { inject as service } from '@ember/service';

export default Router.extend({
	currentUser: service('current-user'),

	willTransition(transition) {
		this._super(...arguments);
		if (!this.currentUser.isLoggedIn) {
			transition.abort();
			this.transitionTo('login');
		}
	},

	didTransition(privateInfos) {
		this._super(...arguments);
		ga.send('pageView', {
			pageName: privateInfos.name,
		});
	},
});

After:

import Router from '@ember/routing/router';
import { inject as service } from '@ember/service';

export default Router.extend({
	currentUser: service('current-user'),

	init() {
		this._super(...arguments);

		this.on('routeWillChange', (transition) => {
			if (!this.currentUser.isLoggedIn) {
				transition.abort();
				this.transitionTo('login');
			}
		});

		this.on('routeDidChange', (transition) => {
			ga.send('pageView', {
				pageName: privateInfos.name,
			});
		});
	},
});