Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 0f1ab76

Browse files
committed
fixup! fix($resource): fix interceptors and success/error callbacks
1 parent 7b23ce5 commit 0f1ab76

File tree

2 files changed

+31
-26
lines changed

2 files changed

+31
-26
lines changed

src/ngResource/resource.js

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,9 @@ function shallowClearAndCopy(src, dst) {
169169
* By default, transformResponse will contain one function that checks if the response looks
170170
* like a JSON string and deserializes it using `angular.fromJson`. To prevent this behavior,
171171
* set `transformResponse` to an empty array: `transformResponse: []`
172-
* - **`cache`** – `{boolean|Cache}` – If true, a default `$http` cache will be used to cache the
173-
* GET request, otherwise if a cache instance built with {@link ng.$cacheFactory $cacheFactory}
174-
* is supplied, this cache will be used for caching.
172+
* - **`cache`** – `{boolean|Cache}` – A boolean value or object created with
173+
* {@link ng.$cacheFactory `$cacheFactory`} to enable or disable caching of the HTTP response.
174+
* See {@link $http#caching $http Caching} for more information.
175175
* - **`timeout`** – `{number}` – Timeout in milliseconds.<br />
176176
* **Note:** In contrast to {@link ng.$http#usage $http.config}, {@link ng.$q promises} are
177177
* **not** supported in `$resource`, because the same value would be used for multiple requests.
@@ -180,13 +180,13 @@ function shallowClearAndCopy(src, dst) {
180180
* cancelled (if not already completed) by calling `$cancelRequest()` on the call's return
181181
* value. Calling `$cancelRequest()` for a non-cancellable or an already completed/cancelled
182182
* request will have no effect.
183-
* - **`withCredentials`** - `{boolean}` - Whether to set the `withCredentials` flag on the
183+
* - **`withCredentials`** `{boolean}` Whether to set the `withCredentials` flag on the
184184
* XHR object. See
185185
* [XMLHttpRequest.withCredentials](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/withCredentials)
186186
* for more information.
187-
* - **`responseType`** - `{string}` - See
187+
* - **`responseType`** `{string}` See
188188
* [XMLHttpRequest.responseType](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseType).
189-
* - **`interceptor`** - `{Object=}` - The interceptor object has four optional methods -
189+
* - **`interceptor`** `{Object=}` The interceptor object has four optional methods -
190190
* `request`, `requestError`, `response`, and `responseError`. See
191191
* {@link ng.$http#interceptors $http interceptors} for details. Note that
192192
* `request`/`requestError` interceptors are applied before calling `$http`, thus before any
@@ -198,9 +198,8 @@ function shallowClearAndCopy(src, dst) {
198198
* response interceptors. Make sure you return an appropriate value and not the `response`
199199
* object passed as input. For example, the default `response` interceptor (which gets applied
200200
* if you don't specify a custom one) returns `response.resource`.
201-
* - **`hasBody`** - `{boolean}` - Allows to specify if a request body should be included or not.
202-
* If not specified only POST, PUT and PATCH requests will have a body.
203-
*
201+
* - **`hasBody`** – `{boolean}` – If true, then the request will have a body.
202+
* If not specified, then only POST, PUT and PATCH requests will have a body. *
204203
* @param {Object} options Hash with custom settings that should extend the
205204
* default `$resourceProvider` behavior. The supported options are:
206205
*
@@ -299,18 +298,18 @@ function shallowClearAndCopy(src, dst) {
299298
*
300299
* @example
301300
*
302-
* ### Credit card resource
301+
* ### Basic usage
303302
*
304303
```js
305-
// Define CreditCard class
306-
var CreditCard = $resource('/user/:userId/card/:cardId',
304+
// Define a CreditCard class
305+
var CreditCard = $resource('/users/:userId/cards/:cardId',
307306
{userId: 123, cardId: '@id'}, {
308307
charge: {method: 'POST', params: {charge: true}}
309308
});
310309
311310
// We can retrieve a collection from the server
312311
var cards = CreditCard.query();
313-
// GET: /user/123/card
312+
// GET: /users/123/cards
314313
// server returns: [{id: 456, number: '1234', name: 'Smith'}]
315314
316315
// Wait for the request to complete
@@ -323,23 +322,25 @@ function shallowClearAndCopy(src, dst) {
323322
// Non-GET methods are mapped onto the instances
324323
card.name = 'J. Smith';
325324
card.$save();
326-
// POST: /user/123/card/456 {id: 456, number: '1234', name: 'J. Smith'}
325+
// POST: /users/123/cards/456 {id: 456, number: '1234', name: 'J. Smith'}
327326
// server returns: {id: 456, number: '1234', name: 'J. Smith'}
328327
329328
// Our custom method is mapped as well (since it uses POST)
330329
card.$charge({amount: 9.99});
331-
// POST: /user/123/card/456?amount=9.99&charge=true {id: 456, number: '1234', name: 'J. Smith'}
330+
// POST: /users/123/cards/456?amount=9.99&charge=true {id: 456, number: '1234', name: 'J. Smith'}
332331
});
333332
334333
// We can create an instance as well
335334
var newCard = new CreditCard({number: '0123'});
336335
newCard.name = 'Mike Smith';
337336
338337
var savePromise = newCard.$save();
339-
// POST: /user/123/card {number: '0123', name: 'Mike Smith'}
338+
// POST: /users/123/cards {number: '0123', name: 'Mike Smith'}
340339
// server returns: {id: 789, number: '0123', name: 'Mike Smith'}
341340
342341
savePromise.then(function() {
342+
// Once the promise is resolved, the created instance
343+
// is populated with the data returned by the server
343344
expect(newCard.id).toEqual(789);
344345
});
345346
```
@@ -352,14 +353,14 @@ function shallowClearAndCopy(src, dst) {
352353
*
353354
* @example
354355
*
355-
* ### User resource
356+
* ### Accessing the response
356357
*
357358
* When the data is returned from the server then the object is an instance of the resource type and
358359
* all of the non-GET methods are available with `$` prefix. This allows you to easily support CRUD
359360
* operations (create, read, update, delete) on server-side data.
360361
*
361362
```js
362-
var User = $resource('/user/:userId', {userId: '@id'});
363+
var User = $resource('/users/:userId', {userId: '@id'});
363364
User.get({userId: 123}).$promise.then(function(user) {
364365
user.abc = true;
365366
user.$save();
@@ -372,7 +373,7 @@ function shallowClearAndCopy(src, dst) {
372373
* the above example and get access to HTTP headers as follows:
373374
*
374375
```js
375-
var User = $resource('/user/:userId', {userId: '@id'});
376+
var User = $resource('/users/:userId', {userId: '@id'});
376377
User.get({userId: 123}, function(user, getResponseHeaders) {
377378
user.abc = true;
378379
user.$save(function(user, putResponseHeaders) {
@@ -384,7 +385,7 @@ function shallowClearAndCopy(src, dst) {
384385
*
385386
* @example
386387
*
387-
* ### Creating a custom 'PUT' request
388+
* ### Creating custom actions
388389
*
389390
* In this example we create a custom method on our resource to make a PUT request:
390391
*
@@ -429,7 +430,7 @@ function shallowClearAndCopy(src, dst) {
429430
*
430431
```js
431432
// ...defining the `Hotel` resource...
432-
var Hotel = $resource('/api/hotel/:id', {id: '@id'}, {
433+
var Hotel = $resource('/api/hotels/:id', {id: '@id'}, {
433434
// Let's make the `query()` method cancellable
434435
query: {method: 'get', isArray: true, cancellable: true}
435436
});
@@ -444,7 +445,7 @@ function shallowClearAndCopy(src, dst) {
444445
}
445446
446447
// Let's query for hotels in `destination`
447-
// (calls: /api/hotel?location=<destination>)
448+
// (calls: /api/hotels?location=<destination>)
448449
this.availableHotels = Hotel.query({location: destination});
449450
};
450451
```

test/ngResource/resourceSpec.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1317,7 +1317,7 @@ describe('basic usage', function() {
13171317
it('should allow per action response interceptor that gets full response', function() {
13181318
var response;
13191319

1320-
$httpBackend.expect('GET', '/CreditCard').respond({id: 1});
1320+
$httpBackend.expect('GET', '/CreditCard').respond(201, {id: 1}, {foo: 'bar'}, 'Ack');
13211321
CreditCard = $resource('/CreditCard', {}, {
13221322
get: {
13231323
method: 'get',
@@ -1329,15 +1329,17 @@ describe('basic usage', function() {
13291329
$httpBackend.flush();
13301330

13311331
expect(response.resource).toBe(cc);
1332-
expect(response.status).toBe(200);
13331332
expect(response.config).toBeDefined();
1333+
expect(response.status).toBe(201);
1334+
expect(response.statusText).toBe('Ack');
1335+
expect(response.headers()).toEqual({foo: 'bar'});
13341336
});
13351337

13361338

13371339
it('should allow per action responseError interceptor that gets full response', function() {
13381340
var response;
13391341

1340-
$httpBackend.expect('GET', '/CreditCard').respond(404);
1342+
$httpBackend.expect('GET', '/CreditCard').respond(404, {ignored: 'stuff'}, {foo: 'bar'}, 'Ack');
13411343
CreditCard = $resource('/CreditCard', {}, {
13421344
get: {
13431345
method: 'get',
@@ -1349,8 +1351,10 @@ describe('basic usage', function() {
13491351
$httpBackend.flush();
13501352

13511353
expect(response.resource).toBe(cc);
1352-
expect(response.status).toBe(404);
13531354
expect(response.config).toBeDefined();
1355+
expect(response.status).toBe(404);
1356+
expect(response.statusText).toBe('Ack');
1357+
expect(response.headers()).toEqual({foo: 'bar'});
13541358
});
13551359

13561360

0 commit comments

Comments
 (0)