This addon adds sane document.title
integration to your ember app.
All it does is include this gist by @machty to your application.
Install by running
ember install:addon ember-cli-document-title
This adds two new keys to your routes:
titleToken
title
They can be either strings or functions.
Every time you transition to a route, the following will happen:
- Ember will collect the
titleToken
s from your leafmost route and bubble them up until it hits a route that hastitle
defined.titleToken
is the name of the route's model by default. - If
title
is a string, that will be used as the document title - If
title
is a function, the collectedtitleToken
s will be passed to it in an array. - What is returned from the
title
function is used as the document title.
If you just put strings as the title
for all your routes, that will be
used as the title for it.
// routes/posts.js
export default Ember.Route.extend({
title: "Our Favorite posts!"
});
// routes/post.js
export default Ember.Route.extend({
title: "Please enjoy this post"
});
Let's say you want something like "Posts - My Blog", with "- My Blog" being static, and "Posts" being something you define on each route.
// routes/posts.js
export default Ember.Route.extend({
titleToken: "Posts"
});
This will be collected and bubble up until it hits the Application Route
// routes/application.js
export default Ember.Route.extend({
title: function(tokens) {
return tokens.join(' - ') + ' - My Blog';
}
});
In this example, we want something like "Name of current post - Posts - My Blog".
Let's say we have this object as our post-model:
Ember.Object.create({
name: "Ember is Omakase"
});
And we want to use the name of each post in the title.
// routes/post.js
export default Ember.Route.extend({
titleToken: function(model) {
return model.get('name');
}
});
This will then bubble up until it reaches our Posts Route:
// routes/posts.js
export default Ember.Route.extend({
titleToken: "Posts"
});
And continue to the Application Route:
// routes/application.js
export default Ember.Route.extend({
title: function(tokens) {
return tokens.reverse().join(' - ') + ' - My Blog';
}
});
This will result in these titles:
- On /posts - "Posts - My Blog"
- On /posts/1 - "Ember is Omakase - Posts - My Blog"