Skip to content
This repository has been archived by the owner on Jan 29, 2024. It is now read-only.

Commit

Permalink
feat(service): add file map lookup into static-files loader
Browse files Browse the repository at this point in the history
This adds support for file cache revisioning.

Usage example: test/unit/service/loader-static-files.spec.js

PR #1593
  • Loading branch information
Jacob Johansen authored and knalli committed Sep 21, 2016
1 parent 0b97766 commit 132e49a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/service/loader-static-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ angular.module('pascalprecht.translate')
* "lang-en_US.json", "lang-de_DE.json", etc. Using this builder,
* the response of these urls must be an object of key-value pairs.
*
* @param {object} options Options object, which gets prefix, suffix and key.
* @param {object} options Options object, which gets prefix, suffix, key, and fileMap
*/
.factory('$translateStaticFilesLoader', $translateStaticFilesLoader);

Expand All @@ -36,12 +36,18 @@ function $translateStaticFilesLoader($q, $http) {
throw new Error('Couldn\'t load static file, no prefix or suffix specified!');
}

var fileUrl = [
file.prefix,
options.key,
file.suffix
].join('');

if (angular.isObject(options.fileMap) && options.fileMap[fileUrl]) {
fileUrl = options.fileMap[fileUrl];
}

return $http(angular.extend({
url: [
file.prefix,
options.key,
file.suffix
].join(''),
url: fileUrl,
method: 'GET',
params: ''
}, options.$http))
Expand Down
14 changes: 14 additions & 0 deletions test/unit/service/loader-static-files.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ describe('pascalprecht.translate', function () {
$translationCache = _$translationCache_;

$httpBackend.when('GET', 'lang_de_DE.json').respond({HEADER: 'Ueberschrift'});
$httpBackend.when('GET', 'lang_de_DE.123.json').respond({HEADER: 'Ueberschrift'});
$httpBackend.when('GET', 'lang_en_US.json').respond({HEADER:'Header'});
$httpBackend.when('GET', 'lang_nt_VD.json').respond(404);
}));
Expand Down Expand Up @@ -62,6 +63,19 @@ describe('pascalprecht.translate', function () {
$httpBackend.flush();
});

it('should fetch static files from file map when invoking', function () {
$httpBackend.expectGET('lang_de_DE.123.json');
$translateStaticFilesLoader({
key: 'de_DE',
prefix: 'lang_',
suffix: '.json',
fileMap : {
'lang_de_DE.json' : 'lang_de_DE.123.json'
}
});
$httpBackend.flush();
});

it('should return a promise', function () {
var promise = $translateStaticFilesLoader({
key: 'de_DE',
Expand Down

0 comments on commit 132e49a

Please sign in to comment.