Skip to content

Commit 983a8c0

Browse files
author
guylabs
committed
Add embeddedResourceName config property and integrate adapted pull request.
#4
1 parent ba172c7 commit 983a8c0

5 files changed

+91
-6
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,25 @@ The `SpringDataRestAdapter` is designed to be configurable and you are able to c
442442
* `linksSelfLinkName` (default: `self`): the name of the self link in the links object.
443443
* `embeddedKey` (default: `_embedded`): the property name where the embedded items are stored.
444444
* `embeddedNewKey` (default: `_embeddedItems`): the property name where the array of embedded items are stored.
445+
* `embeddedNamedResources` (default: `false`): true if the embedded resources names should be left as is, false if they should be removed
446+
* Example if set to true:
447+
```json
448+
{
449+
...
450+
"_embeddedItems": {
451+
"categories": [...]
452+
}
453+
...
454+
}
455+
```
456+
* Example if set to false:
457+
```json
458+
{
459+
...
460+
"_embeddedItems": [...]
461+
...
462+
}
463+
```
445464
* `hrefKey` (default: `href`): the property name where the url is stored under each specific link.
446465
* `resourcesKey` (default: `_resources`): the property name where the resource method is stored.
447466
* `resourcesFunction` (default: `undefined`): the function to use to call the backend. Read more how to do this [here](#exchange-the-underlying-angular-resource-function)
@@ -469,6 +488,7 @@ The config method of the `SpringDataRestAdapterProvider` takes a configuration o
469488
"linksSelfLinkName": "self",
470489
"embeddedKey": "_embedded",
471490
"embeddedNewKey": "_embeddedItems",
491+
"embeddedNamedResources": false,
472492
"resourcesKey": "_resources",
473493
"resourcesFunction": undefined,
474494
"fetchFunction": undefined,

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ angular.module("spring-data-rest").provider("SpringDataRestAdapter", function ()
1515
'linksSelfLinkName': 'self',
1616
'embeddedKey': '_embedded',
1717
'embeddedNewKey': '_embeddedItems',
18+
'embeddedNamedResources': false,
1819
'resourcesKey': '_resources',
1920
'resourcesFunction': undefined,
2021
'fetchFunction': undefined,
@@ -239,11 +240,23 @@ angular.module("spring-data-rest").provider("SpringDataRestAdapter", function ()
239240
}
240241

241242
// process the embedded key and move it to an embedded value key
242-
processedData = moveArray(processedData, config.embeddedKey, config.embeddedNewKey);
243+
processedData = moveArray(processedData, config.embeddedKey, config.embeddedNewKey, config.embeddedNamedResources);
243244

244245
// recursively process all contained objects in the embedded value array
245246
angular.forEach(processedData[config.embeddedNewKey], function (value, key) {
246-
processedData[config.embeddedNewKey][key] = processDataFunction(value, fetchLinkNames, recursive);
247+
248+
// if the embeddedResourceName config variable is set to true, process each resource name array
249+
if (value instanceof Array) {
250+
var processedDataArray = [];
251+
angular.forEach(value, function (arrayValue, arrayKey) {
252+
processedDataArray[arrayKey] = processDataFunction(arrayValue, fetchLinkNames, recursive);
253+
});
254+
processedData[config.embeddedNewKey][key] = processedDataArray;
255+
}
256+
else {
257+
// single objects are processed directly
258+
processedData[config.embeddedNewKey][key] = processDataFunction(value, fetchLinkNames, recursive);
259+
}
247260
});
248261
}
249262

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,27 @@ function deepExtend(destination) {
2020
}
2121

2222
/**
23-
* Moves a key with an array value to the destination key.
23+
* Moves a key with an array value to the destination key. It is also possible to specify to use the value of the
24+
* first key of the object[sourceKey] object instead of the same object.
2425
*
2526
* @param {object} object the object in which the source key exists and destination key is created
2627
* @param {string} sourceKey the source key from which the array is moved
2728
* @param {string} destinationKey the destination key to which the array is moved
29+
* @param {boolean} useSameObject true if the same object is used for the destinationKey, false if the value of the
30+
* first key is used
2831
* @returns {object} the processed object
2932
*/
30-
function moveArray(object, sourceKey, destinationKey) {
33+
function moveArray(object, sourceKey, destinationKey, useSameObject) {
3134
var embeddedObject = object[sourceKey];
3235
if (embeddedObject) {
3336
var key = Object.keys(embeddedObject)[0];
3437
var processedData = {};
35-
processedData[destinationKey] = embeddedObject[key];
38+
39+
if (useSameObject === true) {
40+
processedData[destinationKey] = embeddedObject;
41+
} else {
42+
processedData[destinationKey] = embeddedObject[key];
43+
}
3644

3745
object = angular.extend(object, processedData);
3846
delete object[sourceKey];
@@ -68,7 +76,7 @@ function extractUrl(url, templated) {
6876
function checkUrl(url, resourceName, hrefKey) {
6977
if (url == undefined || !url) {
7078
throw new Error("The provided resource name '" + resourceName + "' has no valid URL in the '" +
71-
hrefKey + "' property.");
79+
hrefKey + "' property.");
7280
}
7381
return url
7482
}

test/angular-spring-data-rest-provider.configuration.spec.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ describe("the configuration", function () {
99
'linksSelfLinkName': 'self',
1010
'embeddedKey': '_embedded',
1111
'embeddedNewKey': '_embeddedItems',
12+
'embeddedNamedResources': false,
1213
'resourcesKey': '_resources',
1314
'resourcesFunction': undefined,
1415
'fetchFunction': undefined,
@@ -36,6 +37,7 @@ describe("the configuration", function () {
3637
'linksSelfLinkName': 'selfNew',
3738
'embeddedKey': '_embeddedNew',
3839
'embeddedNewKey': '_embeddedItemsNew',
40+
'embeddedNamedResources': true,
3941
'resourcesKey': '_resourcesNew',
4042
'resourcesFunction': undefined,
4143
'fetchFunction': undefined,
@@ -59,6 +61,7 @@ describe("the configuration", function () {
5961
'linksSelfLinkName': 'self',
6062
'embeddedKey': '_embedded',
6163
'embeddedNewKey': '_embeddedItemsNew',
64+
'embeddedNamedResources': false,
6265
'resourcesKey': '_resources',
6366
'resourcesFunction': undefined,
6467
'fetchFunction': undefined,
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
describe("the response with the embedded values", function () {
2+
3+
beforeEach(function () {
4+
module('ngResource');
5+
module('spring-data-rest');
6+
7+
// initialize the provider by injecting it to a config block of a test module
8+
// and assign it to the this scope such that it is available in each test
9+
// (see https://jasmine.github.io/2.0/introduction.html#section-The_<code>this</code>_keyword)
10+
angular.module('testModule', function () {
11+
}).config(function (SpringDataRestAdapterProvider) {
12+
SpringDataRestAdapterProvider.config({
13+
'embeddedNamedResources': true
14+
});
15+
springDataRestAdapterProvider = SpringDataRestAdapterProvider;
16+
});
17+
18+
// initialize test module injector
19+
module('testModule');
20+
21+
inject(function (_SpringDataRestAdapter_) {
22+
SpringDataRestAdapter = _SpringDataRestAdapter_;
23+
});
24+
25+
// initialize the configuration, the raw and the processed response
26+
this.config = springDataRestAdapterProvider.config();
27+
this.rawResponse = mockData();
28+
this.response = SpringDataRestAdapter.process(this.rawResponse);
29+
});
30+
31+
it("must have the named resources if the embeddedNamedResources is set to true", function () {
32+
// expect that the first key of the embedded items is categories
33+
var key = Object.keys(this.response[this.config.embeddedNewKey])[0];
34+
expect(key).toBe("categories");
35+
36+
// expect that the embedded key value is an array of the size 2
37+
expect(this.response[this.config.embeddedNewKey]['categories'] instanceof Array).toBe(true);
38+
expect(this.response[this.config.embeddedNewKey]['categories'].length).toBe(2);
39+
});
40+
41+
});

0 commit comments

Comments
 (0)