Skip to content

Commit

Permalink
feat(service) Add regexp matching for route to element transformers (#…
Browse files Browse the repository at this point in the history
…1430)

* Add regexp matching for route to element transformers

* Change https to http for CDN resources in karma
  • Loading branch information
bostrom authored Dec 25, 2016
1 parent d60d599 commit de8f561
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 9 deletions.
37 changes: 28 additions & 9 deletions src/restangular.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ restangular.provider('Restangular', function() {
config.defaultHttpFields = values;
return this;
};

/**
* Always return plain data, no restangularized object
**/
Expand Down Expand Up @@ -440,6 +440,7 @@ restangular.provider('Restangular', function() {
* Add element transformers for certain routes.
*/
config.transformers = config.transformers || {};
config.matchTransformers = config.matchTransformers || [];
object.addElementTransformer = function(type, secondArg, thirdArg) {
var isCollection = null;
var transformer = null;
Expand All @@ -450,17 +451,24 @@ restangular.provider('Restangular', function() {
isCollection = secondArg;
}

var typeTransformers = config.transformers[type];
if (!typeTransformers) {
typeTransformers = config.transformers[type] = [];
}

typeTransformers.push(function(coll, elem) {
var transformerFn = function(coll, elem) {
if (_.isNull(isCollection) || (coll === isCollection)) {
return transformer(elem);
}
return elem;
});
};

if (_.isRegExp(type)) {
config.matchTransformers.push({
regexp: type,
transformer: transformerFn
});
} else {
if (!config.transformers[type]) {
config.transformers[type] = [];
}
config.transformers[type].push(transformerFn);
}

return object;
};
Expand All @@ -477,8 +485,19 @@ restangular.provider('Restangular', function() {
if (!force && !config.transformLocalElements && !elem[config.restangularFields.fromServer]) {
return elem;
}
var typeTransformers = config.transformers[route];

var changedElem = elem;

var matchTransformers = config.matchTransformers;
if (matchTransformers) {
_.each(matchTransformers, function (transformer) {
if (route.match(transformer.regexp)) {
changedElem = transformer.transformer(isCollection, changedElem);
}
});
}

var typeTransformers = config.transformers[route];
if (typeTransformers) {
_.each(typeTransformers, function(transformer) {
changedElem = transformer(isCollection, changedElem);
Expand Down
57 changes: 57 additions & 0 deletions test/restangularSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ describe('Restangular', function() {
$httpBackend.whenJSONP('/accounts/1').respond(accountsModel[1]);
$httpBackend.whenGET('/accounts/1/transactions').respond(accountsModel[1].transactions);
$httpBackend.whenGET('/accounts/1/transactions/1').respond(accountsModel[1].transactions[1]);
$httpBackend.whenGET('/accounts/search/byOwner').respond(accountsModel);

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

$httpBackend.flush();
});

it("should allow for a custom method to be placed at the collection level using a regexp matching the route", function () {
var accountPromise;

Restangular.addElementTransformer(/^accounts/, false, function(model) {
model.prettifyAmount = function() {};
return model;
});

accountsPromise = Restangular.all('accounts/search/byOwner', 1).getList();

accountsPromise.then(function(accounts) {
accounts.forEach(function(account, index) {
expect(typeof account.prettifyAmount).toEqual("function");
});
});

$httpBackend.flush();
});

it("should allow for a custom method to be placed at the model level using regexp route when one model is requested", function() {
var accountPromise;

Restangular.addElementTransformer(/^accounts/, false, function(model) {
model.prettifyAmount = function() {};
return model;
});

accountPromise = Restangular.one('accounts', 1).get();

accountPromise.then(function(account) {
expect(typeof account.prettifyAmount).toEqual("function");
});

$httpBackend.flush();
});

it("should allow for a custom method to be placed at the model level using regexp when several models are requested", function() {
var accountPromise;

Restangular.addElementTransformer(/^accounts/, false, function(model) {
model.prettifyAmount = function() {};
return model;
});

accountsPromise = Restangular.all('accounts', 1).getList();

accountsPromise.then(function(accounts) {
accounts.forEach(function(account, index) {
expect(typeof account.prettifyAmount).toEqual("function");
});
});

$httpBackend.flush();
});

});

describe('extendCollection', function() {
Expand Down

0 comments on commit de8f561

Please sign in to comment.