What is the best way to set the page's title, so that when transitioning between urls, the title will reflect the new state? Is there a way to set the Router to do this?
I want a method that allows me to set a page title schema for each state. So, if the route has parameters, they will be passed into the pageTitle:
sessions : Ember.Route.extend({
route:"/sessions",
connectOutlets : function(router) {
//...
},
pageTitle:function(){
return "Sessions";
},
})
I'm open to any suggestions as to how it is best to implement this type of functionality on the Model or someplace else.
I had the same question.
Note: That you can change the starting title by editting
app/index.html
In lieu of a simple & blessed way (Ember devs are busy on more important things I'm sure), the answer I found most elegant was from CodeJack found here:
Editted: Updated code for Ember 2.0
source: Ember Handlebars Templates in <head> tag
As I struggled a lot to figure out a nice pattern to setup the page title, with the lastest Ember.js (1.0.0 RC7), I decided to create a subclass of
Ember.Route
:Probably this will be natively supported by Ember.js if this pull request is merged: Router now observes the 'title' property on route handlers and sets document.title. Note: this pull request doesn't seem to pass
controller
andmodel
as parameters.If you prefer the Coffee Script equivalent: Ember.js: An easy and clean way to set the page title.
After a long discussion on this pull request it was decided that this didn't belong in core. There is a component and ember-cli addon that implements the functionality.
I used the following method with Ember 1.4.0:
(Similar to Micha Mazaheri's answer.)
You can define a title for each route using the title property. The page title will be automatically updated, and you can call the title in any handlebars template by using {{App.PageTitle.title}}
A quick and dirty approach, in your routes:
I took this approach:
A few issues came up with using navigateTo, this ended up being more reliable and cleaner for my situation at least.