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

Commit

Permalink
feat(translateService): let translate service handle multiple promises
Browse files Browse the repository at this point in the history
Now, when using an async loader, you are able to return a set of promises as well.

Closes #70
  • Loading branch information
0x-r4bbit committed Jun 10, 2013
1 parent ab97c88 commit 0e5d6d9
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -512,12 +512,24 @@ angular.module('pascalprecht.translate').provider('$translate', ['$STORAGE_KEY',
$injector.get($loaderFactory)(angular.extend($loaderOptions, {
key: key
})).then(function (data) {

var translationTable = {};

if (angular.isArray(data)) {
angular.forEach(data, function (table) {
angular.extend(translationTable, table);
});
} else {
angular.extend(translationTable, data);
}

translations(key, translationTable);
$uses = key;
translations(key, data);

if ($storageFactory) {
Storage.set($translate.storageKey(), $uses);
}

$rootScope.$broadcast('translationChangeSuccess');
deferred.resolve($uses);
}, function (key) {
Expand Down
72 changes: 72 additions & 0 deletions test/unit/translateServiceSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -417,4 +417,76 @@ describe('pascalprecht.translate', function () {
});
});
});

describe('using async loaders', function () {

describe('loader returning single promise', function () {
beforeEach(module('pascalprecht.translate', function ($translateProvider, $provide) {

$translateProvider.useLoader('customLoader', {});

$provide.factory('customLoader', ['$q', '$timeout', function ($q, $timeout) {
return function (options) {
var deferred = $q.defer();

$timeout(function () {
deferred.resolve({
FOO: 'bar'
});
}, 1000);

return deferred.promise;
};
}]);

$translateProvider.preferredLanguage('en');
}));

it('should use custom loader', function () {
inject(function ($translate, $timeout) {
$timeout.flush();
expect($translate('FOO')).toEqual('bar');
});
});
});

describe('loader returning multiple promises', function () {

beforeEach(module('pascalprecht.translate', function ($translateProvider, $provide) {

$translateProvider.useLoader('customLoader', {});

$provide.factory('customLoader', ['$q', '$timeout', function ($q, $timeout) {
return function (options) {
var firstDeferred = $q.defer(),
secondDeferred = $q.defer();

$timeout(function () {
firstDeferred.resolve({
FOO: 'bar'
});
});

$timeout(function () {
secondDeferred.resolve({
BAR: 'foo'
});
});

return $q.all([firstDeferred.promise, secondDeferred.promise]);
};
}]);

$translateProvider.preferredLanguage('en');
}));

it('should be able to handle multiple promises', function () {
inject(function ($translate, $timeout) {
$timeout.flush();
expect($translate('FOO')).toEqual('bar');
expect($translate('BAR')).toEqual('foo');
});
});
});
});
});

0 comments on commit 0e5d6d9

Please sign in to comment.