Skip to content

Commit e8f7295

Browse files
madflowbostrom
authored andcommitted
feat(service): add withHttpConfig to objects created with .service
* Replacement for PR #1068 (#1392) * First test for #1068 * Added doc
1 parent c346ae8 commit e8f7295

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,8 @@ app.controller('MainCtrl', function(Restangular, BingRestangular) {
672672

673673
### Decoupled Restangular Service
674674

675-
There're some times where you want to use Restangular but you don't want to expose Restangular object anywhere. For those cases, you can actually use the `service` feature of Restangular.
675+
There're some times where you want to use Restangular but you don't want to expose Restangular object anywhere.
676+
For those cases, you can actually use the `service` feature of Restangular.
676677

677678
Let's see how it works:
678679

@@ -695,6 +696,21 @@ Users.getList().then(function(users) {
695696
})
696697
````
697698

699+
You can also use ```withHttpConfig``` on objects created by ```Restangular.service```.
700+
701+
```js
702+
var personService = Restangular.service('person');
703+
var entity = personService.withHttpConfig({transformRequest: function(data) {
704+
data.fullname = data.firstname + ' ' + data.lastname;
705+
return JSON.stringify(data);
706+
}}).post({
707+
"lastname": "Mueller",
708+
"firstname": "Gerd"
709+
}).then(function(resp) {
710+
console.log(resp);
711+
});
712+
```
713+
698714
We can also use Nested RESTful resources with this:
699715

700716
````js

src/restangular.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,6 +1335,7 @@ restangular.provider('Restangular', function() {
13351335
serv.one = _.bind(one, (parent || service), parent, route);
13361336
serv.post = _.bind(collection.post, collection);
13371337
serv.getList = _.bind(collection.getList, collection);
1338+
serv.withHttpConfig = _.bind(collection.withHttpConfig, collection);
13381339

13391340
for (var prop in collection) {
13401341
if (collection.hasOwnProperty(prop) && _.isFunction(collection[prop]) && !_.includes(knownCollectionMethods, prop)) {

test/restangularSpec.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,15 @@ describe("Restangular", function() {
709709
$httpBackend.expectGET('/accounts/do-something');
710710
$httpBackend.flush();
711711
});
712+
713+
it("should provide a one-off $http configuration method", function() {
714+
var Accounts = Restangular.service('accounts');
715+
Accounts.withHttpConfig({transformRequest: angular.identity});
716+
Accounts.post(newAccount);
717+
$httpBackend.expectPOST('/accounts');
718+
$httpBackend.flush();
719+
});
720+
712721
});
713722

714723
describe("ONE", function() {

0 commit comments

Comments
 (0)