Skip to content

Commit

Permalink
Fix for issue angular#736: ngResource now accepts headers
Browse files Browse the repository at this point in the history
  • Loading branch information
maxmart committed May 17, 2012
1 parent 301d8f2 commit c159b3d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
12 changes: 7 additions & 5 deletions src/ngResource/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
* @param {Object.<Object>=} actions Hash with declaration of custom action that should extend the
* default set of resource actions. The declaration should be created in the following format:
*
* {action1: {method:?, params:?, isArray:?},
* action2: {method:?, params:?, isArray:?},
* {action1: {method:?, params:?, isArray:?, headers:?},
* action2: {method:?, params:?, isArray:?, headers:?},
* ...}
*
* Where:
Expand All @@ -49,6 +49,7 @@
* - `params` – {object=} – Optional set of pre-bound parameters for this action.
* - isArray – {boolean=} – If true then the returned object for this action is an array, see
* `returns` section.
* - `headers` – {object=} – Optional HTTP headers to send
*
* @returns {Object} A resource "class" object with methods for the default set of resource actions
* optionally extended with custom `actions`. The default set contains these actions:
Expand Down Expand Up @@ -130,7 +131,7 @@
* The object returned from this function execution is a resource "class" which has "static" method
* for each action in the definition.
*
* Calling these methods invoke `$http` on the `url` template with the given `method` and `params`.
* Calling these methods invoke `$http` on the `url` template with the given `method`, `params` and `headers`.
* When the data is returned from the server then the object is an instance of the resource type and
* all of the non-GET methods are available with `$` prefix. This allows you to easily support CRUD
* operations (create, read, update, delete) on server-side data.
Expand Down Expand Up @@ -362,7 +363,8 @@ angular.module('ngResource', ['ng']).
$http({
method: action.method,
url: route.url(extend({}, extractParams(data), action.params || {}, params)),
data: data
data: data,
headers: extend({}, action.headers || {})
}).then(function(response) {
var data = response.data;

Expand Down Expand Up @@ -417,4 +419,4 @@ angular.module('ngResource', ['ng']).
}

return ResourceFactory;
}]);
}]);

This comment has been minimized.

Copy link
@vojtajina

vojtajina Jun 5, 2012

please, revert this, to keep new line at the end of the file

19 changes: 16 additions & 3 deletions test/ngResource/resourceSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@ describe("resource", function() {
},
patch: {
method: 'PATCH'
},
conditionalPut: {
method: 'PUT',
headers: {
'If-None-Match': '*'
}
}

});
callback = jasmine.createSpy();
}));
Expand Down Expand Up @@ -56,7 +63,6 @@ describe("resource", function() {
R.get({a:4, b:5, c:6});
});


it('should support escaping colons in url template', function() {
var R = $resource('http://localhost\\:8080/Path/:a/\\:stillPath/:b');

Expand Down Expand Up @@ -145,7 +151,14 @@ describe("resource", function() {
expect(callback.mostRecentCall.args[1]()).toEqual({});
});


it('should send correct headers', function() {

This comment has been minimized.

Copy link
@vojtajina

vojtajina Jun 5, 2012

keep two empty lines in between specs...

$httpBackend.expectPUT('/CreditCard/123', undefined, function(headers) {
return headers['If-None-Match'] == "*";
}).respond({id:123});

CreditCard.conditionalPut({id: {key:123}});

This comment has been minimized.

Copy link
@vojtajina

vojtajina Jun 5, 2012

space after key: 123

});

it("should read partial resource", function() {
$httpBackend.expect('GET', '/CreditCard').respond([{id:{key:123}}]);
var ccs = CreditCard.query();
Expand Down Expand Up @@ -340,4 +353,4 @@ describe("resource", function() {
expect(callback).not.toHaveBeenCalled();
});
});
});
});

This comment has been minimized.

Copy link
@vojtajina

vojtajina Jun 5, 2012

same as above

1 comment on commit c159b3d

@vojtajina
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM only little bit of formatting...

Could you please change commit message according to our convention https://docs.google.com/document/d/1QrDFcIiPjSLDn3EL15IJygNPiHORgU1_OOAqWjiDU5Y/edit to

feat($resource): allow defining headers per action

Closes #736

Thank you very much. We really appreciate that.

Please sign in to comment.