Fix handling of promises for title and description of pages in Quay

We were not correctly waiting for the `scope` on page to be filled before trying to calculate the title, and the logic for description was broken as well.
This commit is contained in:
Joseph Schorr 2017-11-08 16:14:14 -05:00
parent 32022c9421
commit 091b937ee3
2 changed files with 23 additions and 17 deletions

View file

@ -103,9 +103,10 @@ export function provideRun($rootScope: QuayRunScope,
// Listen for route changes and update the title and description accordingly.
$rootScope.$on('$routeChangeSuccess', async(event, current, previous) => {
$rootScope.title = metaService.getTitle(current) || defaultTitle;
const title = await metaService.getTitle(current);
const description = await metaService.getDescription(current);
$rootScope.title = title || defaultTitle;
if ($rootScope.description != description) {
$rootScope.description = description;
}

View file

@ -17,27 +17,32 @@ angular.module('quay').factory('MetaService', ['$interpolate', '$timeout', funct
return inter(page.scope);
};
metaService.getTitle = function(page) {
if (!page || !page.$$route) {
return null;
}
var route = page.$$route;
return interpolate(page, route && route.title);
};
metaService.getDescription = function(page) {
var interpolationPromise = function(page, fieldGetter) {
return new Promise(function(resolve, reject) {
if (!page || !page.$$route) {
resolve(null);
} else {
// Timeout needed because page.scope is undefined
$timeout(function() {
resolve(interpolate(page, page.$$route.description));
}, 10);
return;
}
if (page.scope) {
resolve(interpolate(page, fieldGetter()));
return;
}
// Timeout needed because page.scope is initially undefined.
$timeout(function() {
resolve(interpolationPromise(page, fieldGetter));
}, 10);
});
};
metaService.getTitle = function(page) {
return interpolationPromise(page, () => page.$$route.title);
};
metaService.getDescription = function(page) {
return interpolationPromise(page, () => page.$$route.description);
};
return metaService;
}]);