Giter VIP home page Giter VIP logo

ember-cli-document-title's Introduction

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.10 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) {
   var base = 'My Blog';
   var hasTokens = tokens && tokens.length;

   return hasTokens ? tokens.reverse().join(' - ') + ' - ' + base : base;
  }
});

This will result in these titles:

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

ember-cli-document-title's People

Contributors

kimroen avatar rwjblue avatar mnutt avatar artsyca avatar

Watchers

Rami Rizk avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.