Skip to content

Commit

Permalink
Add support for promise titleTokens
Browse files Browse the repository at this point in the history
  • Loading branch information
poohitan committed Mar 27, 2017
1 parent 0c3b992 commit 756be32
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 10 deletions.
15 changes: 15 additions & 0 deletions tests/acceptance/document-title-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
1 change: 1 addition & 0 deletions tests/dummy/app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Router.map(function() {
this.route('team');
this.route('candy');
this.route('friendship-status', function() {});
this.route('promise');
});

export default Router;
11 changes: 11 additions & 0 deletions tests/dummy/app/routes/promise.js
Original file line number Diff line number Diff line change
@@ -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);
});
}
});
31 changes: 21 additions & 10 deletions vendor/document-title/document-title.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 756be32

Please sign in to comment.