diff --git a/src/restangular.js b/src/restangular.js index 493a1f47..39c5c18d 100644 --- a/src/restangular.js +++ b/src/restangular.js @@ -253,7 +253,7 @@ module.provider('Restangular', function() { config.getUrlFromElem = function(elem) { return config.getFieldFromElem(config.restangularFields.selfLink, elem); }; - + config.useCannonicalId = _.isUndefined(config.useCannonicalId) ? false : config.useCannonicalId; object.setUseCannonicalId = function(value) { config.useCannonicalId = value; @@ -557,7 +557,6 @@ module.provider('Restangular', function() { add += what; url += add; } - if (this.config.suffix && url.indexOf(this.config.suffix, url.length - this.config.suffix.length) === -1 && !this.config.getUrlFromElem(current)) { @@ -636,7 +635,9 @@ module.provider('Restangular', function() { var elemSelfLink = __this.config.getUrlFromElem(elem); if (elemSelfLink) { if (__this.config.isAbsoluteUrl(elemSelfLink)) { - return elemSelfLink; + return (__this.config.absoluteUrl && __this.config.absoluteUrl !== true) + ? __this.config.absoluteUrl.replace(/\/$/, '') + "/" + elemSelfLink.replace(/^\//, '') //add the absolute URL to the start if set + : elemSelfLink; } else { elemUrl = elemSelfLink; } @@ -661,6 +662,8 @@ module.provider('Restangular', function() { } } } + //remove any trailing suffix from the url before concatinating new subresources + if (__this.config.suffix) acum = acum.replace(new RegExp(__this.config.suffix + '$'), ''); return acum.replace(/\/$/, '') + '/' + elemUrl; diff --git a/test/restangularSpec.js b/test/restangularSpec.js index 6dcad6ba..5a541766 100644 --- a/test/restangularSpec.js +++ b/test/restangularSpec.js @@ -24,10 +24,18 @@ describe("Restangular", function() { {from: "Anonymous", amount: 0.1416, id: 1, _links: {self: "/accountsHAL/paul/transactions/1"}} ], _links: {self: "/accountsHAL/paul"}} ]; + + accountsModelWithSuffix = [ + {id: 0, user: "Martin", amount: 42, transaction: [], href: "/accountsSuffix/martin.json"}, + {id: 1, user: "Paul", amount: 3.1416, transaction: [ + {from: "Martin", amount: 3, id: 0, href: "/accounts/paul/transactions/0.json"}, + {from: "Anonymous", amount: 0.1416, id: 1, href: "/accounts/paul/transactions/1.json"} + ], href: "/accounts/paul.json"} + ]; infoModel = { id: 0, text: "Some additional account information" - } + }; newAccount = {id: 44, user: "First User", amount: 45, transactions: []}; @@ -63,6 +71,14 @@ describe("Restangular", function() { accountsHalModel[0] = angular.fromJson(data); return [200, data, ""]; }); + + $httpBackend.whenGET("/accountsSuffix").respond(accountsModelWithSuffix); + $httpBackend.whenGET("/accountsSuffix.json").respond(accountsModelWithSuffix); + $httpBackend.whenGET("/accountsSuffix/martin.json").respond(accountsModelWithSuffix[0]); + $httpBackend.whenPUT("/accountsSuffix/martin/transactions.json").respond(function(method, url, data) { + accountsModelWithSuffix[0].transaction = angular.fromJson(data); + return [200, data, ""]; + }); // Full URL $httpBackend.whenGET('http://accounts.com/all').respond(accountsModel); @@ -745,6 +761,37 @@ describe("Restangular", function() { $httpBackend.flush(); }); }); + + describe("Self linking with suffix", function() { + it("Should allow for suffix in selfLinks", function() { + var linkRestangular = Restangular.withConfig(function(RestangularConfigurer) { + RestangularConfigurer.setRequestSuffix('.json'); + }); + + var arr = linkRestangular.all('accountsSuffix').getList().$object; + $httpBackend.flush(); + + var account = arr[0]; + var transactions = account.all('transactions'); + expect(transactions.getRestangularUrl()).toEqual("/accountsSuffix/martin/transactions"); + }); + }); + + describe("Cross-site selfLinks", function() { + it("Should add the absolute URL to the self link", function() { + var linkRestangular = Restangular.withConfig(function(RestangularConfigurer) { + RestangularConfigurer.setRequestSuffix('.json'); + RestangularConfigurer.setSelfLinkAbsoluteUrl('http://accounts.com/'); + }); + + var arr = linkRestangular.all('accountsSuffix').getList().$object; + $httpBackend.flush(); + + var account = arr[0]; + expect(account.getRestangularUrl()).toEqual("http://accounts.com/accountsSuffix/martin.json"); + expect(account.all('transactions').getRestangularUrl()).toEqual("http://accounts.com/accountsSuffix/martin/transactions"); + }); + }); describe("Singe one (endpoint not expecting an id)", function() { it('does not use the id for single resource GET', function() {