Skip to content

Commit de8f561

Browse files
authored
feat(service) Add regexp matching for route to element transformers (#1430)
* Add regexp matching for route to element transformers * Change https to http for CDN resources in karma
1 parent d60d599 commit de8f561

File tree

2 files changed

+85
-9
lines changed

2 files changed

+85
-9
lines changed

src/restangular.js

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ restangular.provider('Restangular', function() {
6565
config.defaultHttpFields = values;
6666
return this;
6767
};
68-
68+
6969
/**
7070
* Always return plain data, no restangularized object
7171
**/
@@ -440,6 +440,7 @@ restangular.provider('Restangular', function() {
440440
* Add element transformers for certain routes.
441441
*/
442442
config.transformers = config.transformers || {};
443+
config.matchTransformers = config.matchTransformers || [];
443444
object.addElementTransformer = function(type, secondArg, thirdArg) {
444445
var isCollection = null;
445446
var transformer = null;
@@ -450,17 +451,24 @@ restangular.provider('Restangular', function() {
450451
isCollection = secondArg;
451452
}
452453

453-
var typeTransformers = config.transformers[type];
454-
if (!typeTransformers) {
455-
typeTransformers = config.transformers[type] = [];
456-
}
457-
458-
typeTransformers.push(function(coll, elem) {
454+
var transformerFn = function(coll, elem) {
459455
if (_.isNull(isCollection) || (coll === isCollection)) {
460456
return transformer(elem);
461457
}
462458
return elem;
463-
});
459+
};
460+
461+
if (_.isRegExp(type)) {
462+
config.matchTransformers.push({
463+
regexp: type,
464+
transformer: transformerFn
465+
});
466+
} else {
467+
if (!config.transformers[type]) {
468+
config.transformers[type] = [];
469+
}
470+
config.transformers[type].push(transformerFn);
471+
}
464472

465473
return object;
466474
};
@@ -477,8 +485,19 @@ restangular.provider('Restangular', function() {
477485
if (!force && !config.transformLocalElements && !elem[config.restangularFields.fromServer]) {
478486
return elem;
479487
}
480-
var typeTransformers = config.transformers[route];
488+
481489
var changedElem = elem;
490+
491+
var matchTransformers = config.matchTransformers;
492+
if (matchTransformers) {
493+
_.each(matchTransformers, function (transformer) {
494+
if (route.match(transformer.regexp)) {
495+
changedElem = transformer.transformer(isCollection, changedElem);
496+
}
497+
});
498+
}
499+
500+
var typeTransformers = config.transformers[route];
482501
if (typeTransformers) {
483502
_.each(typeTransformers, function(transformer) {
484503
changedElem = transformer(isCollection, changedElem);

test/restangularSpec.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ describe('Restangular', function() {
5454
$httpBackend.whenJSONP('/accounts/1').respond(accountsModel[1]);
5555
$httpBackend.whenGET('/accounts/1/transactions').respond(accountsModel[1].transactions);
5656
$httpBackend.whenGET('/accounts/1/transactions/1').respond(accountsModel[1].transactions[1]);
57+
$httpBackend.whenGET('/accounts/search/byOwner').respond(accountsModel);
5758

5859
$httpBackend.whenGET('/info').respond(infoModel);
5960
$httpBackend.whenGET('/accounts/1/info').respond(infoModel);
@@ -950,6 +951,62 @@ describe('Restangular', function() {
950951

951952
$httpBackend.flush();
952953
});
954+
955+
it("should allow for a custom method to be placed at the collection level using a regexp matching the route", function () {
956+
var accountPromise;
957+
958+
Restangular.addElementTransformer(/^accounts/, false, function(model) {
959+
model.prettifyAmount = function() {};
960+
return model;
961+
});
962+
963+
accountsPromise = Restangular.all('accounts/search/byOwner', 1).getList();
964+
965+
accountsPromise.then(function(accounts) {
966+
accounts.forEach(function(account, index) {
967+
expect(typeof account.prettifyAmount).toEqual("function");
968+
});
969+
});
970+
971+
$httpBackend.flush();
972+
});
973+
974+
it("should allow for a custom method to be placed at the model level using regexp route when one model is requested", function() {
975+
var accountPromise;
976+
977+
Restangular.addElementTransformer(/^accounts/, false, function(model) {
978+
model.prettifyAmount = function() {};
979+
return model;
980+
});
981+
982+
accountPromise = Restangular.one('accounts', 1).get();
983+
984+
accountPromise.then(function(account) {
985+
expect(typeof account.prettifyAmount).toEqual("function");
986+
});
987+
988+
$httpBackend.flush();
989+
});
990+
991+
it("should allow for a custom method to be placed at the model level using regexp when several models are requested", function() {
992+
var accountPromise;
993+
994+
Restangular.addElementTransformer(/^accounts/, false, function(model) {
995+
model.prettifyAmount = function() {};
996+
return model;
997+
});
998+
999+
accountsPromise = Restangular.all('accounts', 1).getList();
1000+
1001+
accountsPromise.then(function(accounts) {
1002+
accounts.forEach(function(account, index) {
1003+
expect(typeof account.prettifyAmount).toEqual("function");
1004+
});
1005+
});
1006+
1007+
$httpBackend.flush();
1008+
});
1009+
9531010
});
9541011

9551012
describe('extendCollection', function() {

0 commit comments

Comments
 (0)