Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions src/restangular.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ restangular.provider('Restangular', function() {
config.restangularFields = config.restangularFields || {
id: 'id',
route: 'route',
custom: 'custom',
parentResource: 'parentResource',
restangularCollection: 'restangularCollection',
cannonicalId: '__cannonicalId',
Expand Down Expand Up @@ -693,6 +694,11 @@ restangular.provider('Restangular', function() {
elemUrl += '/' + (__this.config.encodeIds ? encodeURIComponent(elemId) : elemId);
}
}

var custom = elem[__this.config.restangularFields.custom];
if (custom) {
elemUrl += '/' + custom;
}
}
acum = acum.replace(/\/$/, '') + '/' + elemUrl;
return __this.normalizeUrl(acum);
Expand Down Expand Up @@ -782,8 +788,11 @@ restangular.provider('Restangular', function() {
var urlHandler = new config.urlCreatorFactory[config.urlCreator]();
urlHandler.setConfig(config);

function restangularizeBase(parent, elem, route, reqParams, fromServer) {
function restangularizeBase(parent, elem, route, reqParams, fromServer, custom) {
elem[config.restangularFields.route] = route;
if (custom) {
elem[config.restangularFields.custom] = custom;
}
elem[config.restangularFields.getRestangularUrl] = _.bind(urlHandler.fetchUrl, urlHandler, elem);
elem[config.restangularFields.getRequestedUrl] = _.bind(urlHandler.fetchRequestedUrl, urlHandler, elem);
elem[config.restangularFields.addRestangularMethod] = _.bind(addRestangularMethodFunction, elem);
Expand Down Expand Up @@ -965,10 +974,10 @@ restangular.provider('Restangular', function() {
copiedElement, copiedElement[config.restangularFields.route], copiedElement[config.restangularFields.fromServer]);
}

function restangularizeElem(parent, element, route, fromServer, collection, reqParams) {
function restangularizeElem(parent, element, route, fromServer, collection, reqParams, custom) {
var elem = config.onBeforeElemRestangularized(element, false, route);

var localElem = restangularizeBase(parent, elem, route, reqParams, fromServer);
var localElem = restangularizeBase(parent, elem, route, reqParams, fromServer, custom);

if (config.useCannonicalId) {
localElem[config.restangularFields.cannonicalId] = config.getIdFromElem(localElem);
Expand Down Expand Up @@ -996,10 +1005,10 @@ restangular.provider('Restangular', function() {
return config.transformElem(localElem, false, route, service, true);
}

function restangularizeCollection(parent, element, route, fromServer, reqParams) {
function restangularizeCollection(parent, element, route, fromServer, reqParams, custom) {
var elem = config.onBeforeElemRestangularized(element, true, route);

var localElem = restangularizeBase(parent, elem, route, reqParams, fromServer);
var localElem = restangularizeBase(parent, elem, route, reqParams, fromServer, custom);
localElem[config.restangularFields.restangularCollection] = true;
localElem[config.restangularFields.post] = _.bind(postFunction, localElem, null);
localElem[config.restangularFields.remove] = _.bind(deleteFunction, localElem);
Expand Down Expand Up @@ -1113,7 +1122,8 @@ restangular.provider('Restangular', function() {
processedData,
what,
true,
fullParams
fullParams,
what
),
filledArray
);
Expand All @@ -1126,7 +1136,8 @@ restangular.provider('Restangular', function() {
processedData,
__this[config.restangularFields.route],
true,
fullParams
fullParams,
what
),
filledArray
);
Expand Down Expand Up @@ -1198,7 +1209,8 @@ restangular.provider('Restangular', function() {
route,
true,
null,
fullParams
fullParams,
what
);
resolvePromise(deferred, response, data, filledObject);
} else {
Expand All @@ -1208,7 +1220,8 @@ restangular.provider('Restangular', function() {
__this[config.restangularFields.route],
true,
null,
fullParams
fullParams,
what
);

data[config.restangularFields.singleOne] = __this[config.restangularFields.singleOne];
Expand Down
21 changes: 14 additions & 7 deletions test/restangularSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ describe('Restangular', function() {
$httpBackend.when('OPTIONS', '/accounts').respond();

$httpBackend.whenGET('/accounts').respond(accountsModel);
$httpBackend.whenGET('/accounts/do-something').respond(accountsDoSomethingModel);
$httpBackend.whenGET('/accounts/do-something?param=value').respond(accountsDoSomethingModel);
$httpBackend.whenJSONP('/accounts').respond(accountsModel);
$httpBackend.whenGET('/accounts/0,1').respond(accountsModel);
$httpBackend.whenGET('/accounts/messages').respond(messages);
$httpBackend.whenGET('/accounts/1/message').respond(messages[0]);
$httpBackend.whenGET('/accounts/messages?param=value').respond(messages);
$httpBackend.whenGET('/accounts/1/message?param=value').respond(messages[0]);
$httpBackend.whenGET('/accounts/1/messages').respond(messages);
$httpBackend.whenGET('/accounts/0').respond(accountsModel[0]);
$httpBackend.whenGET('/accounts/1').respond(accountsModel[1]);
Expand Down Expand Up @@ -565,8 +565,10 @@ describe('Restangular', function() {
});

it('Custom GET methods should work', function() {
restangularAccounts.customGETLIST('messages').then(function(msgs) {
restangularAccounts.customGETLIST('messages', { param: "value" }).then(function(msgs) {
expect(Restangular.stripRestangular(msgs)).toEqual(Restangular.stripRestangular(messages));
expect(msgs.getRestangularUrl()).toBe("/accounts/messages");
expect(msgs.getRequestedUrl()).toBe("/accounts/messages?param=value");
});

$httpBackend.flush();
Expand Down Expand Up @@ -726,12 +728,15 @@ describe('Restangular', function() {
Accounts.post(newAccount);
Accounts.one(0).get();
Accounts.getList();
Accounts.doSomething();
Accounts.doSomething({ param: "value" }).then(function(model) {
expect(Restangular.stripRestangular(model)).toEqual(Restangular.stripRestangular(accountsDoSomethingModel));
expect(model.getRestangularUrl()).toBe("/accounts/do-something");
expect(model.getRequestedUrl()).toBe("/accounts/do-something?param=value");
});

$httpBackend.expectPOST('/accounts');
$httpBackend.expectGET('/accounts/0');
$httpBackend.expectGET('/accounts');
$httpBackend.expectGET('/accounts/do-something');
$httpBackend.flush();
});

Expand Down Expand Up @@ -807,8 +812,10 @@ describe('Restangular', function() {


it('Custom GET methods should work', function() {
restangularAccount1.customGET('message').then(function(msg) {
restangularAccount1.customGET('message', { param: "value" }).then(function(msg) {
expect(Restangular.stripRestangular(msg)).toEqual(Restangular.stripRestangular(messages[0]));
expect(msg.getRestangularUrl()).toBe("/accounts/23/message");
expect(msg.getRequestedUrl()).toBe("/accounts/23/message?param=value");
});

$httpBackend.flush();
Expand Down