npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@jasonmit/ember-cli-document-title

v0.4.1

Published

Adding document title functionality to your ember app

Downloads

2

Readme

Sane Document Title Build Status Ember Observer Score Code Climate

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

Install by running

ember install ember-cli-document-title

So, how does this work?

This adds two new keys to your routes:

  1. titleToken
  2. title

They can be either strings or functions.

Every time you transition to a route, the following will happen:

  1. Ember will collect the titleTokens from your leafmost route and bubble them up until it hits a route that has title defined. titleToken is the name of the route's model by default.
  2. If title is a string, that will be used as the document title
  3. If title is a function, the collected titleTokens will be passed to it in an array.
  4. What is returned from the title function is used as the document title.

Examples!

Simple, static titles

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'
});

Dynamic segments with a static part

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';
  }
});

Dynamic title based on model info

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:

  • On /posts - "Posts - My Blog"
  • On /posts/1 - "Ember is Omakase - Posts - My Blog"

Async titles using promises

You may be in a situation where it makes sense to have one or more of your titleTokens 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's an example where we return a promise that resolves to the post name plus the author name 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:

  • On /posts/1 - "Ember is Omakase (by John Smith) - Posts - My Blog"

It's worth noting that the page title will not update until all the promises have resolved.

Use with ember-cli-head

Using ember-cli-document-title with ember-cli-head is very straight forward and allows you to use the wonderful route based declarative API for title 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:

  • Install both addons:
ember install ember-cli-head
ember install ember-cli-document-title
  • Add 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):

{{! app/templates/head.hbs }}

<title>{{model.title}}</title>