Skip to content

Instantly share code, notes, and snippets.

@balinterdi
Created July 3, 2019 13:59
Show Gist options
  • Save balinterdi/d9c71c60253f7237a042f18eed82cff7 to your computer and use it in GitHub Desktop.
Save balinterdi/d9c71c60253f7237a042f18eed82cff7 to your computer and use it in GitHub Desktop.
Setting document title in the application route
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
export default Route.extend({
currentUser: service(),
router: service(),
init() {
this._super();
this.setDocumentTitle();
},
setDocumentTitle() {
this.router.on('routeDidChange', (transition) => {
let toRouteName = transition.to.name;
let pageTitles = {
'bands.index': () => {
return 'Bands';
},
'bands.band.songs': () => {
let bandRouteInfo = transition.to.find(info => info.name.includes('bands.band'));
let bandId = bandRouteInfo.params.id;
let band = this.store.peekRecord('band', bandId);
let name = capitalizeWords(band.name);
return `${name} songs`;
}
}
let titleSegments = [];
let titleSetter = pageTitles[toRouteName];
if (titleSetter) {
titleSegments.push(titleSetter());
}
titleSegments.push('Rock and Roll with Ember.js');
document.title = titleSegments.join(' - ');
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment