This addon adds sane document.title
integration to your ember app.
Originally based on this gist by @machty, and since improved upon by many fabulous contributors.
Tested to work with Ember 1.13.13 and up.
Install by running
ember install 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:
titleToken
s from your leafmost route andbubble them up until it hits a route that has title
defined.titleToken
is the name of the route's model by default.title
is a string, that will be used as the document titletitle
is a function, the collected titleToken
s will be passedto it in an array.title
function is used as the documenttitle.If you just put strings as the title
for all your routes, that will beused 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) {
tokens = Ember.makeArray(tokens);
tokens.unshift('My Blog');
return tokens.reverse().join(' - ');
}
});
This will result in these titles:
You may be in a situation where it makes sense to have one or more of your titleToken
s be asynchronous. For example if a related model is async, or you just enjoy working with Promises in your past-time.
Luckily, you can return a promise from any of your titleToken
functions, and they will all be resolved by the time your title
function receives them.
An example! Let's say we have these two Ember Data models; a post
and its user
:
// models/post.js
export default DS.Model.extend({
name: DS.attr('string'),
author: DS.belongsTo('user', { async: true })
});
// models/user.js
export default DS.Model.extend({
firstName: DS.attr('string'),
lastName: DS.attr('string')
});
In our document title, we want the name of the author in parenthesis along with the post title.
The author
relationship is async
, so getting it will return a promise. Here'san example where we return a promise that resolves to the post name plus the authorname in parenthesis:
// routes/post.js
export default Ember.Route.extend({
titleToken: function(model) {
var postName = model.get('name');
return model.get('author')
.then(function (user) {
var authorName = user.get('firstName') + user.get('lastName');
return postName + '(by ' + authorName + ')';
});
}
});
With the same configuration for Posts
and Application
routes as in the previous example, this will result in this title:
It's worth noting that the page title will not update until all the promises have resolved.
ember-cli-head
Using ember-cli-document-title
with ember-cli-headis very straight forward and allows you to use the wonderful route based declarative API fortitle
and still easily add other things to the document's <head>
(i.e. meta
tags).
Only a few tweaks are needed to use both of these addons together:
ember install ember-cli-head
ember install ember-cli-document-title
headData
and setTitle
to your app/router.js
:const Router = Ember.Router.extend({
location: config.locationType,
headData: Ember.inject.service(),
setTitle(title) {
this.get('headData').set('title', title);
}
});
Remove <title>
from your app/index.html
.
Update app/templates/head.hbs
(added by ember-cli-head):
删除老版本的ember-cli npm uninstall -g ember-cli 清除 NPM 缓存 npm cache clean 清除 Bower cache bower cache clean 下载你想要的ember-cli版本,代替 npm install -g ember-cli@X.X.X
Ember CLI 是一个 Ember.js 命令行工具,提供了由 broccoli 提供的快速的资源管道和项目结构。 Ember CLI 基于 Ember App Kit Project 目前已经废弃。 Assets Compilation Ember CLI asset compilation is based on broccoli. Broccoli has support for: Ha
This repository is no longer maintained. As a replacement check out: https://github.com/sir-dunxalot/ember-tooltips Ember CLI Tooltipster An Ember CLI add-on that wraps Tooltipster into an ember compo
ember-cli-updater This ember-cli addon helps you update your ember-cli application or addon. The idea of this addon is to automate some parts of the upgrade process so it's simplified. Not every chang
Ember-cli-yadda This Ember CLI addon facilitates writing BDD tests in the Gherkin language and executing them against your Ember app. @mschinis (Micheal Schinis) Did a great talk at @emberlondon BDD a
Ember-cli-simditor Ember component wrapper for simditor. Changes 0.0.7 Different from previous version, you must wrap content in object. See issue 6 for why. Getting Started Installation In your ember
ember-cli-chai Chai assertions for Ember.js. Deprecated This package is deprecated. Please use ember-auto-import to use chai and chai plugins directly. If you'd like to use chai, or were previously us