Skip to content

Commit 1a22096

Browse files
author
guylabs
committed
#9 - Add possibility to pass URL template within resource name
1 parent e97d28d commit 1a22096

File tree

6 files changed

+115
-5
lines changed

6 files changed

+115
-5
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,17 @@ This will call *Angular* `$resource` method by default (this is [exchangeable](#
207207
* `actions`: custom action that should extend the default set of the `$resource` actions. Read more [here](https://docs.angularjs.org/api/ngResource/service/$resource).
208208
* `options`: custom settings that should extend the default `$resourceProvider` behavior Read more [here](https://docs.angularjs.org/api/ngResource/service/$resource).
209209

210+
You are also able to add URL templates to the passed in `linkName` parameter. (This just works if you pass a string and not a resource object) The following example explains the usage:
211+
212+
```javascript
213+
SpringDataRestAdapter.process(response).then(function(processedResponse) {
214+
var resources = processedResponse._resources("self/:id", {id: "@id"});
215+
var item = resources.get({id: 1}).then(function(data) {
216+
item = data;
217+
};
218+
});
219+
```
220+
210221
The `_resources` method returns the *Angular* resource "class" object with methods for the default set of resource actions. Read more [here](https://docs.angularjs.org/api/ngResource/service/$resource).
211222
212223
If no parameter is given the `_resources` method will return all available resources objects of the given object. When for example the following JSON response object is given:

RELEASENOTES.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
# Release notes of angular-spring-data-rest
22

3+
## Version 0.4.2
4+
5+
* Tag: [0.4.2](https://github.com/guylabs/angular-spring-data-rest/tree/0.4.2)
6+
* Release: [angular-spring-data-rest-0.4.2.zip](https://github.com/guylabs/angular-spring-data-rest/releases/download/0.4.2/angular-spring-data-rest-0.4.2.zip)
7+
8+
### Changes
9+
10+
* Fixed issue [#9](https://github.com/guylabs/angular-spring-data-rest/issues/9).
11+
12+
### Migration notes
13+
14+
* No migration needed.
15+
316
## Version 0.4.1
417

518
* Tag: [0.4.1](https://github.com/guylabs/angular-spring-data-rest/tree/0.4.1)

dist/angular-spring-data-rest.js

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,15 @@ angular.module("spring-data-rest").provider("SpringDataRestAdapter", function ()
160160
var resources = this[config.linksKey];
161161
var parameters = paramDefaults;
162162

163+
var urlTemplates = "";
164+
165+
// split the resourceObject to extract the URL templates for the $resource method
166+
if(hasUrlTemplate(resourceObject)) {
167+
var extractedUrlTemplates = extractUrlTemplates(resourceObject);
168+
resourceObject = extractedUrlTemplates[0];
169+
urlTemplates = extractedUrlTemplates[1];
170+
}
171+
163172
// if a resource object is given process it
164173
if (angular.isObject(resourceObject)) {
165174
if (!resourceObject.name) {
@@ -193,8 +202,8 @@ angular.module("spring-data-rest").provider("SpringDataRestAdapter", function ()
193202
return resourcesFunction(getProcessedUrl(data, resourceObject.name), parameters, actions, options);
194203
} else if (resourceObject in resources) {
195204

196-
// process the url and call the resources function with the given parameters
197-
return resourcesFunction(getProcessedUrl(data, resourceObject), parameters, actions, options);
205+
// process the url, add the url templates and call the resources function with the given parameters
206+
return resourcesFunction(getProcessedUrl(data, resourceObject) + urlTemplates, parameters, actions, options);
198207
}
199208

200209
// return the available resources as resource object array if the resource object parameter is not set
@@ -325,6 +334,27 @@ angular.module("spring-data-rest").provider("SpringDataRestAdapter", function ()
325334
// extract the template parameters of the raw URL
326335
return extractUrl(rawUrl, data[config.linksKey][resourceName].templated);
327336
}
337+
338+
/**
339+
* Returns true if the resource name has URL templates
340+
* @param resourceName the resource name to parse
341+
* @returns {boolean} true if the resource name has URL templates, false otherwise
342+
*/
343+
function hasUrlTemplate(resourceName) {
344+
return typeof resourceName == "string" && resourceName.indexOf("/") > 0;
345+
}
346+
347+
/**
348+
* Extracts the URL template and returns the resource name and the URL templates as an array.
349+
* @param resourceName the resource name to parse
350+
* @returns {[]} the first element is the raw resource name and the second is the extracted URL templates
351+
*/
352+
function extractUrlTemplates(resourceName) {
353+
if(hasUrlTemplate(resourceName)) {
354+
var indexOfSlash = resourceName.indexOf("/");
355+
return [resourceName.substr(0, indexOfSlash), resourceName.substr(indexOfSlash, resourceName.length)];
356+
}
357+
}
328358
};
329359

330360
// return an object with the processData function

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: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,15 @@ angular.module("spring-data-rest").provider("SpringDataRestAdapter", function ()
148148
var resources = this[config.linksKey];
149149
var parameters = paramDefaults;
150150

151+
var urlTemplates = "";
152+
153+
// split the resourceObject to extract the URL templates for the $resource method
154+
if(hasUrlTemplate(resourceObject)) {
155+
var extractedUrlTemplates = extractUrlTemplates(resourceObject);
156+
resourceObject = extractedUrlTemplates[0];
157+
urlTemplates = extractedUrlTemplates[1];
158+
}
159+
151160
// if a resource object is given process it
152161
if (angular.isObject(resourceObject)) {
153162
if (!resourceObject.name) {
@@ -181,8 +190,8 @@ angular.module("spring-data-rest").provider("SpringDataRestAdapter", function ()
181190
return resourcesFunction(getProcessedUrl(data, resourceObject.name), parameters, actions, options);
182191
} else if (resourceObject in resources) {
183192

184-
// process the url and call the resources function with the given parameters
185-
return resourcesFunction(getProcessedUrl(data, resourceObject), parameters, actions, options);
193+
// process the url, add the url templates and call the resources function with the given parameters
194+
return resourcesFunction(getProcessedUrl(data, resourceObject) + urlTemplates, parameters, actions, options);
186195
}
187196

188197
// return the available resources as resource object array if the resource object parameter is not set
@@ -313,6 +322,27 @@ angular.module("spring-data-rest").provider("SpringDataRestAdapter", function ()
313322
// extract the template parameters of the raw URL
314323
return extractUrl(rawUrl, data[config.linksKey][resourceName].templated);
315324
}
325+
326+
/**
327+
* Returns true if the resource name has URL templates
328+
* @param resourceName the resource name to parse
329+
* @returns {boolean} true if the resource name has URL templates, false otherwise
330+
*/
331+
function hasUrlTemplate(resourceName) {
332+
return typeof resourceName == "string" && resourceName.indexOf("/") > 0;
333+
}
334+
335+
/**
336+
* Extracts the URL template and returns the resource name and the URL templates as an array.
337+
* @param resourceName the resource name to parse
338+
* @returns {[]} the first element is the raw resource name and the second is the extracted URL templates
339+
*/
340+
function extractUrlTemplates(resourceName) {
341+
if(hasUrlTemplate(resourceName)) {
342+
var indexOfSlash = resourceName.indexOf("/");
343+
return [resourceName.substr(0, indexOfSlash), resourceName.substr(indexOfSlash, resourceName.length)];
344+
}
345+
}
316346
};
317347

318348
// return an object with the processData function

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,5 +440,31 @@ describe("the resources property", function () {
440440
this.rootScope.$apply();
441441
});
442442

443+
it("it must extract the URL templates and add it as suffix to the proper URL", function () {
444+
var resourcesKey = this.config.resourcesKey;
445+
var url = undefined;
446+
var resourcesFunctionConfiguration = {
447+
'resourcesFunction': function (inUrl) {
448+
url = inUrl;
449+
return 'foo';
450+
}
451+
};
452+
453+
// define the resource name and the correct resource href url
454+
var resourceObject = "self/:id";
455+
var resourceHref = "http://localhost:8080/categories";
456+
457+
// set the new resource function with the given parameters
458+
springDataRestAdapterProvider.config(resourcesFunctionConfiguration);
459+
SpringDataRestAdapter.process(this.rawResponse).then(function (processedData) {
460+
// call the new resource method and expect the response and the call to the method
461+
var resourceResponse = processedData[resourcesKey](resourceObject);
462+
expect(resourceResponse).toEqual('foo');
463+
expect(url).toEqual(resourceHref + '/:id');
464+
});
465+
466+
this.rootScope.$apply();
467+
});
468+
443469
});
444470

0 commit comments

Comments
 (0)