fix($resource): forwardport exposing headers from 0.9.19

This commit is contained in:
Igor Minar 2011-12-01 16:20:08 -05:00
parent 1d14760c6d
commit 44b2f44f93
3 changed files with 30 additions and 17 deletions

View file

@ -107,23 +107,24 @@ ResourceFactory.prototype = {
}
var value = this instanceof Resource ? this : (action.isArray ? [] : new Resource(data));
var future = self.$http({
self.$http({
method: action.method,
url: route.url(extend({}, extractParams(data), action.params || {}, params)),
data: data
}).then(function(response) {
response = response.data;
if (response) {
var data = response.data;
if (data) {
if (action.isArray) {
value.length = 0;
forEach(response, function(item) {
forEach(data, function(item) {
value.push(new Resource(item));
});
} else {
copy(response, value);
copy(data, value);
}
}
(success||noop)(value);
(success||noop)(value, response.headers);
}, error);
return value;

View file

@ -141,13 +141,17 @@
</pre>
*
* It's worth noting that the success callback for `get`, `query` and other method gets passed
* in the response that came from the server, so one could rewrite the above example as:
* in the response that came from the server as well as $http header getter function, so one
* could rewrite the above example and get access to http headers as:
*
<pre>
var User = $resource('/user/:userId', {userId:'@id'});
User.get({userId:123}, function(u){
User.get({userId:123}, function(u, getResponseHeaders){
u.abc = true;
u.$save();
u.$save(function(u, putResponseHeaders) {
//u => saved user object
//putResponseHeaders => $http header getter
});
});
</pre>

View file

@ -107,7 +107,9 @@ describe("resource", function() {
$httpBackend.flush();
nakedExpect(cc).toEqual({id: 123, name: 'misko'});
expect(callback).toHaveBeenCalledWith(cc);
expect(callback).toHaveBeenCalledOnce();
expect(callback.mostRecentCall.args[0]).toEqual(cc);
expect(callback.mostRecentCall.args[1]()).toEqual({});
}));
it("should read resource", inject(function($httpBackend) {
@ -120,7 +122,8 @@ describe("resource", function() {
$httpBackend.flush();
nakedExpect(cc).toEqual({id: 123, number: '9876'});
expect(callback).toHaveBeenCalledWith(cc);
expect(callback.mostRecentCall.args[0]).toEqual(cc);
expect(callback.mostRecentCall.args[1]()).toEqual({});
}));
it("should read partial resource", inject(function($httpBackend) {
@ -137,7 +140,8 @@ describe("resource", function() {
$httpBackend.expect('GET', '/CreditCard/123').respond({id: {key: 123}, number: '9876'});
cc.$get(callback);
$httpBackend.flush();
expect(callback).toHaveBeenCalledWith(cc);
expect(callback.mostRecentCall.args[0]).toEqual(cc);
expect(callback.mostRecentCall.args[1]()).toEqual({});
expect(cc.number).toEqual('9876');
}));
@ -160,7 +164,8 @@ describe("resource", function() {
$httpBackend.flush();
nakedExpect(ccs).toEqual([{id:1}, {id:2}]);
expect(callback).toHaveBeenCalledWith(ccs);
expect(callback.mostRecentCall.args[0]).toEqual(ccs);
expect(callback.mostRecentCall.args[1]()).toEqual({});
}));
it("should have all arguments optional", inject(function($httpBackend) {
@ -180,7 +185,8 @@ describe("resource", function() {
expect(callback).not.toHaveBeenCalled();
$httpBackend.flush();
nakedExpect(callback.mostRecentCall.args).toEqual([{}]);
nakedExpect(callback.mostRecentCall.args[0]).toEqual({});
expect(callback.mostRecentCall.args[1]()).toEqual({});
callback.reset();
$httpBackend.expect('DELETE', '/CreditCard/333').respond(204, null);
@ -188,7 +194,8 @@ describe("resource", function() {
expect(callback).not.toHaveBeenCalled();
$httpBackend.flush();
nakedExpect(callback.mostRecentCall.args).toEqual([{}]);
nakedExpect(callback.mostRecentCall.args[0]).toEqual({});
expect(callback.mostRecentCall.args[1]()).toEqual({});
}));
it('should post charge verb', inject(function($httpBackend) {
@ -205,7 +212,7 @@ describe("resource", function() {
}));
it('should create on save', inject(function($httpBackend) {
$httpBackend.expect('POST', '/CreditCard', '{"name":"misko"}').respond({id: 123});
$httpBackend.expect('POST', '/CreditCard', '{"name":"misko"}').respond({id: 123}, {header1: 'a'});
var cc = new CreditCard();
expect(cc.$get).toBeDefined();
@ -219,7 +226,8 @@ describe("resource", function() {
$httpBackend.flush();
nakedExpect(cc).toEqual({id:123});
expect(callback).toHaveBeenCalledWith(cc);
expect(callback.mostRecentCall.args[0]).toEqual(cc);
expect(callback.mostRecentCall.args[1]()).toEqual({header1: 'a'});
}));
it('should not mutate the resource object if response contains no body', inject(function($httpBackend) {