Skip to content

Commit 72359f9

Browse files
committed
Update the usage of the new process function with promises
1 parent 171da5f commit 72359f9

File tree

1 file changed

+58
-35
lines changed

1 file changed

+58
-35
lines changed

README.md

Lines changed: 58 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,12 @@ To use the `SpringDataRestAdapter` object you need to include the `angular-sprin
143143
var myApp = angular.module("myApplication", ["ngResource", "spring-data-rest"]);
144144
```
145145

146-
Now you are able use the `SpringDataRestAdapter` object and process a given response:
146+
Now you are able use the `SpringDataRestAdapter` object and process a given response and you will get a promise back which resolves with the processes response:
147147

148148
```javascript
149-
var processedResponse = SpringDataRestAdapter.process(response);
149+
SpringDataRestAdapter.process(response).then(function(processedResponse) {
150+
...
151+
});
150152
```
151153

152154
Please read on on how to use the `_resources` method and the `_embeddedItems` property to ease the handling of resources and embedded items.
@@ -168,13 +170,14 @@ var response = {
168170
}
169171
...
170172
}
171-
var processedResponse = SpringDataRestAdapter.process(response);
172173
```
173174

174-
Then the `SpringDataRestAdapter` will add the `_resources` method to the same level such that you can call it the following way:
175+
Then the `SpringDataRestAdapter` will add the `_resources` method to the same level such that you can call it the following way (inside the `then` function of the promise):
175176

176177
```javascript
177-
processedResponse._resources(linkName, paramDefaults, actions, options);
178+
SpringDataRestAdapter.process(response).then(function(processedResponse) {
179+
processedResponse._resources(linkName, paramDefaults, actions, options);
180+
});
178181
```
179182

180183
This `_resources` method is added recursively to all the properties of the JSON response object where a `_links` property exists.
@@ -186,14 +189,16 @@ The `_resources` method takes the following four parameters:
186189
* `linkName`: the name of the link's `href` you want to call with the underlying *Angular* `$resource` function. You can also pass in a resource object with parameters in the following way:
187190

188191
```javascript
189-
var resourceObject = {
192+
SpringDataRestAdapter.process(response).then(function(processedResponse) {
193+
var resourceObject = {
190194
"name": "self",
191195
"parameters": {
192196
"size": 20,
193197
"sort": "asc"
194198
}
195-
}
196-
processedResponse._resources(resourceObject, paramDefaults, actions, options);
199+
}
200+
processedResponse._resources(resourceObject, paramDefaults, actions, options);
201+
});
197202
```
198203

199204
This will call *Angular* `$resource` method by default (this is [exchangeable](#exchange-the-underlying-angular-resource-function)) with the `href` of the `self` resource and will add the parameters `size` and `sort` as query string to the URL. If the resource object parameters and the `paramDefaults` parameters are set, then these two objects are merged such that the resource object parameters appear first in the new object and the `paramDefaults` parameters last.
@@ -222,12 +227,13 @@ var response = {
222227
}
223228
...
224229
}
225-
var processedResponse = SpringDataRestAdapter.process(response);
226230
```
227231
Then the following call to the `_resources` method without any parameter will return an array of all available resource objects.
228232

229233
```javascript
230-
var availableResources = processedResponse._resources();
234+
SpringDataRestAdapter.process(response).then(function(processedResponse) {
235+
var availableResources = processedResponse._resources();
236+
});
231237
```
232238

233239
The above call will result in the following return value:
@@ -255,13 +261,14 @@ This functionality is useful if you want to first check all available resources
255261
This example refers to the JSON response in the [Overview](#overview). If you want to get the parent category of a category you would call the `_resources` method the following way:
256262

257263
```javascript
258-
var processedResponse = SpringDataRestAdapter.process(response);
259-
var parentCategoryResource = processedResponse._embeddedItems[0]._resources("parentCategory");
264+
SpringDataRestAdapter.process(response).then(function(processedResponse) {
265+
var parentCategoryResource = processedResponse._embeddedItems[0]._resources("parentCategory");
260266

261-
// create a GET request, with the help of the Angular resource class, to the parent category
262-
// url and log the response to the console
263-
var parentCategory = parentCategoryResource.get(function() {
267+
// create a GET request, with the help of the Angular resource class, to the parent category
268+
// url and log the response to the console
269+
var parentCategory = parentCategoryResource.get(function() {
264270
console.log("Parent category name: " + parentCategory.name);
271+
});
265272
});
266273
```
267274

@@ -294,11 +301,12 @@ The `_embeddedItems` property is just a convention property created by the `Spri
294301
This example refers to the JSON response in the [Overview](#overview). If you want to iterate over all categories in the response you would do it in the following way:
295302

296303
```javascript
297-
var processedResponse = SpringDataRestAdapter.process(response);
304+
SpringDataRestAdapter.process(response).then(function(processedResponse) {
298305

299-
// log the name of all categories contained in the response to the console
300-
angular.forEach(processedResponse._embeddedItems, function (category, key) {
306+
// log the name of all categories contained in the response to the console
307+
angular.forEach(processedResponse._embeddedItems, function (category, key) {
301308
console.log("Category name: " + category.name);
309+
});
302310
});
303311
```
304312

@@ -325,40 +333,41 @@ The `SpringDataRestAdapter` is able to fetch specified links automatically. This
325333
and you want to fetch the data from the `anotherLink` link then you just need to pass the link name to the `SpringDataRestAdapter` process function:
326334

327335
```javascript
328-
var processedResponse = SpringDataRestAdapter.process(response, 'anotherLink');
329-
```
330-
331-
Now you are able to get the data from the processed resource by just accessing the property named `anotherLink`:
332-
333-
```javascript
334-
processedResponse.anotherLink
336+
SpringDataRestAdapter.process(response, 'anotherLink').then(function(processedResponse) {
337+
var fetchedObject = processedResponse.anotherLink;
338+
});
335339
```
340+
Now you are able to get the data from the processed resource by just accessing the property named `anotherLink`.
336341

337342
The `SpringDataRestAdapter` by default adds the response of the link to a property in the original response with the same name as the link.
338343

339344
If you want to process the returned response again with the `SpringDataRestAdapter` then you are able to set the `recursive` flag when creating it:
340345

341346
```javascript
342-
var processedResponse = SpringDataRestAdapter.process(response, 'anotherLink', true);
347+
SpringDataRestAdapter.process(response, 'anotherLink', true).then(function(processedResponse) {
348+
...
349+
});
343350
```
344351

345352
Now the response of the `anotherLink` will be processed the same way as the main response was processed. But *be aware* when setting the recursive flag to true, because when your reponses of the links contain the same link name again, then it will end up in a infinite loop.
346353

347-
348354
It will not fetch the `self` link as this would make no sense because the data is already in the response. The `self` key is also configurable. Read more [here](#configuration-of-the-springdatarestadapter).
349355

350356
#### Fetch multiple or all links
351357

352358
If you want to fetch multiple links then you are able to add an array of strings with the given link names:
353359

354360
```javascript
355-
var processedResponse = SpringDataRestAdapter.process(response, ['anotherLink', 'testLink']);
361+
SpringDataRestAdapter.process(response, ['anotherLink', 'testLink']).then(function(processedResponse) {
362+
...
363+
});
356364
```
357-
358365
and if you want to fetch all links, then you can use the predefined and also configurable `fetchAllLinkNamesKey`:
359366

360367
```javascript
361-
var processedResponse = SpringDataRestAdapter.process(response, '_allLinks');
368+
SpringDataRestAdapter.process(response, '_allLinks').then(function(processedResponse) {
369+
...
370+
});
362371
```
363372

364373
Please read more [here](#configuration-of-the-springdatarestadapter) on how to configure the `fetchAllLinkNamesKey`.
@@ -395,7 +404,7 @@ The parameters for the fetch method are the following:
395404

396405
### How to use `SpringDataRestAdapter` with promises
397406

398-
The `SpringDataRestAdapter` is also able to process promises instead of data objects. The data object which is passed to the specified promise when it is resolved needs to be in the following format:
407+
The `SpringDataRestAdapter` is also able to process promises instead of data objects. The data object which is passed to the specified promise when it is resolved needs to be in the following formats:
399408

400409
```javascript
401410
{
@@ -414,17 +423,31 @@ The `SpringDataRestAdapter` is also able to process promises instead of data obj
414423
}
415424
}
416425
```
426+
or
427+
```javascript
428+
{
429+
"_links": {
430+
"self": {
431+
"href": "http://localhost:8080/categories{?page,size,sort}",
432+
"templated": true
433+
},
434+
"anotherLink": {
435+
"href": "http://localhost:8080/anotherLink"
436+
}
437+
},
438+
// the rest of the JSON response
439+
...
440+
}
441+
```
417442

418-
The `data` property of the promise object is the JSON response of the back end. To process such a promise you need to call the `SpringDataRestAdapter` like in the following example:
443+
The `data` property of the second format of the promise object is the JSON response of the back end. To process such a promise you need to call the `SpringDataRestAdapter` like in the following example:
419444

420445
```javascript
421-
SpringDataRestAdapter.processWithPromise(promise).then(function(processedResponse) {
446+
SpringDataRestAdapter.process(promise).then(function(processedResponse) {
422447
// you can now use the processedResponse as any other processed response from the SpringDataRestAdapter
423448
};
424449
```
425450
426-
The only change between the `SpringDataRestAdapter.processWithPromise` and the `SpringDataRestAdapter.process` methods is the changed type of the data object. All other parameters are the same.
427-
428451
You can also right away use the promise support with the `Angular` `$http.get()` method like in the following example:
429452
430453
```javascript

0 commit comments

Comments
 (0)