diff --git a/tests/acceptance/document-title-test.js b/tests/acceptance/document-title-test.js index f7ce671..8296e82 100644 --- a/tests/acceptance/document-title-test.js +++ b/tests/acceptance/document-title-test.js @@ -93,3 +93,18 @@ test('title updates when you switch routes', function(assert) { assert.equal(document.title, 'Ember is omakase - Posts - My Blog'); }); }); + +test('promise title is set after promise is resolved', function(assert) { + assert.expect(1); + + var done = assert.async(); + + visit('/promise'); + + andThen(function() { + setTimeout(function () { + assert.equal(document.title, 'This title is as async as possible - My Blog'); + done(); + }, 4000); + }); +}); diff --git a/tests/dummy/app/router.js b/tests/dummy/app/router.js index 0d41d85..cb1c7d8 100644 --- a/tests/dummy/app/router.js +++ b/tests/dummy/app/router.js @@ -11,6 +11,7 @@ Router.map(function() { this.route('team'); this.route('candy'); this.route('friendship-status', function() {}); + this.route('promise'); }); export default Router; diff --git a/tests/dummy/app/routes/promise.js b/tests/dummy/app/routes/promise.js new file mode 100644 index 0000000..8bfd0ea --- /dev/null +++ b/tests/dummy/app/routes/promise.js @@ -0,0 +1,11 @@ +import Ember from 'ember'; + +export default Ember.Route.extend({ + titleToken() { + return new Ember.RSVP.Promise(function(resolve) { + setTimeout(function () { + resolve('This title is as async as possible'); + }, 3000); + }); + } +}); diff --git a/vendor/document-title/document-title.js b/vendor/document-title/document-title.js index e7469ba..394cdf8 100644 --- a/vendor/document-title/document-title.js +++ b/vendor/document-title/document-title.js @@ -1,5 +1,6 @@ var get = Ember.get; var getOwner = Ember.getOwner; +var Promise = Ember.RSVP.Promise; var routeProps = { // `titleToken` can either be a static string or a function @@ -52,17 +53,27 @@ routeProps[mergedActionPropertyName] = { // token-collection, and the title is decided right here. var title = get(this, 'title'); if (title) { - var finalTitle; - if (typeof title === 'function') { - finalTitle = title.call(this, tokens); - } else { - // Tokens aren't even considered... a string - // title just sledgehammer overwrites any children tokens. - finalTitle = title; - } + var self = this; - // Stubbable fn that sets document.title - this.router.setTitle(finalTitle); + // Wrap in promise in case some tokens are asynchronous. + Promise.resolve() + .then(function() { + if (typeof title === 'function') { + // Wait for all tokens to resolve. It resolves immediately if all tokens are plain values (not promises). + return Promise.all(tokens) + .then(function(resolvedTokens) { + return title.call(self, resolvedTokens); + }); + } else { + // Tokens aren't even considered... a string + // title just sledgehammer overwrites any children tokens. + return title; + } + }) + .then(function(finalTitle) { + // Stubbable fn that sets document.title + self.router.setTitle(finalTitle); + }); } else { // Continue bubbling. return true;