Skip to content

Commit dae715b

Browse files
author
Romy Kusuma
committed
Update naming variables
1. Update naming convention 2. Use config attribute to get self href
1 parent 398a535 commit dae715b

File tree

3 files changed

+43
-23
lines changed

3 files changed

+43
-23
lines changed

dist/angular-spring-data-rest.js

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,12 @@ angular.module("spring-data-rest").provider("SpringDataRestAdapter", function ()
6767
},
6868

6969
$get: ["$injector", function ($injector) {
70-
// map contain self link as key and a list of linkNames which were already fetched
71-
var map = {};
70+
/**
71+
* Link map which contains the 'self' link as key and the list of already fetched link names as value.
72+
* This is used to check that every link on each entity is only fetched once to avoid an infinite recursive loop.
73+
* @type {{Object}}
74+
*/
75+
var linkMap = {};
7276

7377
/**
7478
* Returns the Angular $resource method which is configured with the given parameters.
@@ -252,9 +256,11 @@ angular.module("spring-data-rest").provider("SpringDataRestAdapter", function ()
252256

253257
// if there are links to fetch, then process and fetch them
254258
if (fetchLinkNames != undefined) {
255-
var self = data._links.self.href;
256-
if (!map[self]) {
257-
map[self] = [];
259+
var self = data[config.linksKey][config.linksSelfLinkName][config.linksHrefKey];
260+
261+
// add the self link value as key and add an empty map to store all other links which are fetched for this entity
262+
if (!linkMap[self]) {
263+
linkMap[self] = [];
258264
}
259265

260266
// process all links
@@ -264,16 +270,17 @@ angular.module("spring-data-rest").provider("SpringDataRestAdapter", function ()
264270
if (linkName != config.linksSelfLinkName) {
265271

266272
// check if:
267-
// 1. the all link names key is given then fetch the link
268-
// 2. the given key is equal
269-
// 3. the given key is inside the array
270-
if (map[self].indexOf(linkName) < 0 &&
273+
// 1. the link was not fetched already
274+
// 2. the all link names key is given then fetch the link
275+
// 3. the given key is equal
276+
// 4. the given key is inside the array
277+
if (linkMap[self].indexOf(linkName) < 0 &&
271278
(fetchLinkNames == config.fetchAllKey ||
272279
(typeof fetchLinkNames === "string" && linkName == fetchLinkNames) ||
273280
(fetchLinkNames instanceof Array && fetchLinkNames.indexOf(linkName) >= 0))) {
274281
promisesArray.push(fetchFunction(getProcessedUrl(data, linkName), linkName,
275282
processedData, fetchLinkNames, recursive));
276-
map[self].push(linkName);
283+
linkMap[self].push(linkName);
277284
}
278285
}
279286
});
@@ -368,8 +375,14 @@ angular.module("spring-data-rest").provider("SpringDataRestAdapter", function ()
368375
}
369376
};
370377

378+
// empty the map and
371379
// return an object with the processData function
372-
return {process: processData};
380+
return {
381+
process: function(promiseOrData, fetchLinkNames, recursive) {
382+
linkMap = {};
383+
return processData(promiseOrData, fetchLinkNames, recursive);
384+
}
385+
};
373386
}]
374387
};
375388

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.

src/angular-spring-data-rest-provider.js

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,12 @@ angular.module("spring-data-rest").provider("SpringDataRestAdapter", function ()
5555
},
5656

5757
$get: ["$injector", function ($injector) {
58-
// map contain self link as key and a list of linkNames which were already fetched
59-
var map = {};
58+
/**
59+
* Link map which contains the 'self' link as key and the list of already fetched link names as value.
60+
* This is used to check that every link on each entity is only fetched once to avoid an infinite recursive loop.
61+
* @type {{Object}}
62+
*/
63+
var linkMap = {};
6064

6165
/**
6266
* Returns the Angular $resource method which is configured with the given parameters.
@@ -240,9 +244,11 @@ angular.module("spring-data-rest").provider("SpringDataRestAdapter", function ()
240244

241245
// if there are links to fetch, then process and fetch them
242246
if (fetchLinkNames != undefined) {
243-
var self = data._links.self.href;
244-
if (!map[self]) {
245-
map[self] = [];
247+
var self = data[config.linksKey][config.linksSelfLinkName][config.linksHrefKey];
248+
249+
// add the self link value as key and add an empty map to store all other links which are fetched for this entity
250+
if (!linkMap[self]) {
251+
linkMap[self] = [];
246252
}
247253

248254
// process all links
@@ -252,16 +258,17 @@ angular.module("spring-data-rest").provider("SpringDataRestAdapter", function ()
252258
if (linkName != config.linksSelfLinkName) {
253259

254260
// check if:
255-
// 1. the all link names key is given then fetch the link
256-
// 2. the given key is equal
257-
// 3. the given key is inside the array
258-
if (map[self].indexOf(linkName) < 0 &&
261+
// 1. the link was not fetched already
262+
// 2. the all link names key is given then fetch the link
263+
// 3. the given key is equal
264+
// 4. the given key is inside the array
265+
if (linkMap[self].indexOf(linkName) < 0 &&
259266
(fetchLinkNames == config.fetchAllKey ||
260267
(typeof fetchLinkNames === "string" && linkName == fetchLinkNames) ||
261268
(fetchLinkNames instanceof Array && fetchLinkNames.indexOf(linkName) >= 0))) {
262269
promisesArray.push(fetchFunction(getProcessedUrl(data, linkName), linkName,
263270
processedData, fetchLinkNames, recursive));
264-
map[self].push(linkName);
271+
linkMap[self].push(linkName);
265272
}
266273
}
267274
});
@@ -360,7 +367,7 @@ angular.module("spring-data-rest").provider("SpringDataRestAdapter", function ()
360367
// return an object with the processData function
361368
return {
362369
process: function(promiseOrData, fetchLinkNames, recursive) {
363-
map = {};
370+
linkMap = {};
364371
return processData(promiseOrData, fetchLinkNames, recursive);
365372
}
366373
};

0 commit comments

Comments
 (0)