Created
July 3, 2019 13:59
-
-
Save balinterdi/d9c71c60253f7237a042f18eed82cff7 to your computer and use it in GitHub Desktop.
Setting document title in the application route
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
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