Skip to content

Commit a35885b

Browse files
author
guylabs
committed
#23(feat): add fetchMultiple flag to resolve the same link multiple times
1 parent 8b90c30 commit a35885b

File tree

6 files changed

+124
-23
lines changed

6 files changed

+124
-23
lines changed

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,10 +373,21 @@ SpringDataRestAdapter.process(response, 'anotherLink', true).then(function(proce
373373
});
374374
```
375375
376-
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.
376+
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 responses of the links contain the same link name again, then it will end up in a infinite loop.
377377
378378
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).
379379
380+
As a last configuration option you are able to set the fourth parameter called `fetchMultiple` of the `process` method which is used to define if a link is fetched multiple times if the same link is contained in multiple links and you want to resolve each one of them. The drawback
381+
is that it won't cache the responses in any way. So if you enable this then multiple network calls will be made. Here an example:
382+
383+
```javascript
384+
SpringDataRestAdapter.process(response, 'anotherLink', true, true).then(function(processedResponse) {
385+
...
386+
});
387+
```
388+
389+
:warning: If you set the `fetchMultiple` to `true` you could end up in an infinite loop if you have circular dependencies in your `_links`.
390+
380391
#### Fetch multiple or all links
381392
382393
If you want to fetch multiple links then you are able to add an array of strings with the given link names:
@@ -552,7 +563,7 @@ myApp.config(function (SpringDataRestAdapterProvider) {
552563
SpringDataRestAdapterProvider.config({
553564
fetchFunction: function (url, key, data, fetchLinkNames, recursive) {
554565
var $http = angular.injector(['ng']).get('$http');
555-
566+
556567
$http.get('/rest/endpoint').then(function (responseData) {
557568
console.log(responseData);
558569
})

dist/angular-spring-data-rest.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,11 @@ angular.module("spring-data-rest").provider("SpringDataRestAdapter", function ()
100100
* @param {object} data the data object reference in which the response is stored
101101
* @param {[string]|string} fetchLinkNames the fetch link names to allow to process the fetched response
102102
* @param {boolean} recursive true if the fetched response should be processed recursively with the
103+
* @param {boolean} fetchMultiple true if multiple same link names should be resolved. ATTENTION: this could lead to
104+
* an infinite loop when you have circular dependencies between the links.
103105
* adapter, false otherwise
104106
*/
105-
function fetchFunction(url, key, data, fetchLinkNames, recursive) {
107+
function fetchFunction(url, key, data, fetchLinkNames, recursive, fetchMultiple) {
106108
if (config.fetchFunction == undefined) {
107109
var promisesArray = [];
108110

@@ -111,7 +113,7 @@ angular.module("spring-data-rest").provider("SpringDataRestAdapter", function ()
111113

112114
// wrap the response again with the adapter and return the promise
113115
if (recursive) {
114-
return processData(responseData.data, fetchLinkNames, true).then(function (processedData) {
116+
return processData(responseData.data, fetchLinkNames, true, fetchMultiple).then(function (processedData) {
115117
data[key] = processedData;
116118
});
117119
} else {
@@ -129,7 +131,7 @@ angular.module("spring-data-rest").provider("SpringDataRestAdapter", function ()
129131
// wait for all promises to be resolved and return a new promise
130132
return $injector.get("$q").all(promisesArray);
131133
} else {
132-
return config.fetchFunction(url, key, data, fetchLinkNames, recursive);
134+
return config.fetchFunction(url, key, data, fetchLinkNames, recursive, fetchMultiple);
133135
}
134136
}
135137

@@ -142,9 +144,11 @@ angular.module("spring-data-rest").provider("SpringDataRestAdapter", function ()
142144
* 'fetchAllLinkNamesKey' key from the config object to fetch all links except the 'self' key.
143145
* @param {boolean} recursive true if the automatically fetched response should be processed recursively with the
144146
* adapter, false otherwise
147+
* @param {boolean} fetchMultiple true if multiple same link names should be resolved. ATTENTION: this could lead to
148+
* an infinite loop when you have circular dependencies between the links.
145149
* @returns {object} the processed JSON data
146150
*/
147-
var processData = function processDataFunction(promiseOrData, fetchLinkNames, recursive) {
151+
var processData = function processDataFunction(promiseOrData, fetchLinkNames, recursive, fetchMultiple) {
148152

149153
// convert the given promise or data to a $q promise
150154
return $injector.get("$q").when(promiseOrData).then(function (data) {
@@ -274,12 +278,12 @@ angular.module("spring-data-rest").provider("SpringDataRestAdapter", function ()
274278
// 2. the all link names key is given then fetch the link
275279
// 3. the given key is equal
276280
// 4. the given key is inside the array
277-
if (linkMap[self].indexOf(linkName) < 0 &&
281+
if ((fetchMultiple || linkMap[self].indexOf(linkName) < 0) &&
278282
(fetchLinkNames == config.fetchAllKey ||
279283
(typeof fetchLinkNames === "string" && linkName == fetchLinkNames) ||
280284
(fetchLinkNames instanceof Array && fetchLinkNames.indexOf(linkName) >= 0))) {
281285
promisesArray.push(fetchFunction(getProcessedUrl(data, linkName), linkName,
282-
processedData, fetchLinkNames, recursive));
286+
processedData, fetchLinkNames, recursive, fetchMultiple));
283287
linkMap[self].push(linkName);
284288
}
285289
}
@@ -307,7 +311,7 @@ angular.module("spring-data-rest").provider("SpringDataRestAdapter", function ()
307311
var processedDataArrayPromise;
308312
angular.forEach(value, function (arrayValue, arrayKey) {
309313
if (angular.isObject(arrayValue)) {
310-
processedDataArrayPromise = processDataFunction({data: arrayValue}, fetchLinkNames, recursive).then(function (processedResponseData) {
314+
processedDataArrayPromise = processDataFunction({data: arrayValue}, fetchLinkNames, recursive, fetchMultiple).then(function (processedResponseData) {
311315
processedDataArray[arrayKey] = processedResponseData;
312316
});
313317
promisesArray.push(processedDataArrayPromise);
@@ -324,7 +328,7 @@ angular.module("spring-data-rest").provider("SpringDataRestAdapter", function ()
324328
}
325329
} else if (angular.isObject(value)) {
326330
// single objects are processed directly
327-
promisesArray.push(processDataFunction({data: value}, fetchLinkNames, recursive).then(function (processedResponseData) {
331+
promisesArray.push(processDataFunction({data: value}, fetchLinkNames, recursive, fetchMultiple).then(function (processedResponseData) {
328332
processedData[config.embeddedNewKey][key] = processedResponseData;
329333
}));
330334
}
@@ -378,9 +382,9 @@ angular.module("spring-data-rest").provider("SpringDataRestAdapter", function ()
378382
// empty the map and
379383
// return an object with the processData function
380384
return {
381-
process: function(promiseOrData, fetchLinkNames, recursive) {
385+
process: function (promiseOrData, fetchLinkNames, recursive, fetchMultiple) {
382386
linkMap = {};
383-
return processData(promiseOrData, fetchLinkNames, recursive);
387+
return processData(promiseOrData, fetchLinkNames, recursive, fetchMultiple);
384388
}
385389
};
386390
}]

dist/angular-spring-data-rest.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)