Skip to content

Commit

Permalink
#9 - Add possibility to pass URL template within resource name
Browse files Browse the repository at this point in the history
  • Loading branch information
guylabs committed Apr 22, 2015
1 parent e97d28d commit 1a22096
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 5 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,17 @@ This will call *Angular* `$resource` method by default (this is [exchangeable](#
* `actions`: custom action that should extend the default set of the `$resource` actions. Read more [here](https://docs.angularjs.org/api/ngResource/service/$resource).
* `options`: custom settings that should extend the default `$resourceProvider` behavior Read more [here](https://docs.angularjs.org/api/ngResource/service/$resource).

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:

```javascript
SpringDataRestAdapter.process(response).then(function(processedResponse) {
var resources = processedResponse._resources("self/:id", {id: "@id"});
var item = resources.get({id: 1}).then(function(data) {
item = data;
};
});
```
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).
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:
Expand Down
13 changes: 13 additions & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# Release notes of angular-spring-data-rest

## Version 0.4.2

* Tag: [0.4.2](https://github.com/guylabs/angular-spring-data-rest/tree/0.4.2)
* 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)

### Changes

* Fixed issue [#9](https://github.com/guylabs/angular-spring-data-rest/issues/9).

### Migration notes

* No migration needed.

## Version 0.4.1

* Tag: [0.4.1](https://github.com/guylabs/angular-spring-data-rest/tree/0.4.1)
Expand Down
34 changes: 32 additions & 2 deletions dist/angular-spring-data-rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,15 @@ angular.module("spring-data-rest").provider("SpringDataRestAdapter", function ()
var resources = this[config.linksKey];
var parameters = paramDefaults;

var urlTemplates = "";

// split the resourceObject to extract the URL templates for the $resource method
if(hasUrlTemplate(resourceObject)) {
var extractedUrlTemplates = extractUrlTemplates(resourceObject);
resourceObject = extractedUrlTemplates[0];
urlTemplates = extractedUrlTemplates[1];
}

// if a resource object is given process it
if (angular.isObject(resourceObject)) {
if (!resourceObject.name) {
Expand Down Expand Up @@ -193,8 +202,8 @@ angular.module("spring-data-rest").provider("SpringDataRestAdapter", function ()
return resourcesFunction(getProcessedUrl(data, resourceObject.name), parameters, actions, options);
} else if (resourceObject in resources) {

// process the url and call the resources function with the given parameters
return resourcesFunction(getProcessedUrl(data, resourceObject), parameters, actions, options);
// process the url, add the url templates and call the resources function with the given parameters
return resourcesFunction(getProcessedUrl(data, resourceObject) + urlTemplates, parameters, actions, options);
}

// return the available resources as resource object array if the resource object parameter is not set
Expand Down Expand Up @@ -325,6 +334,27 @@ angular.module("spring-data-rest").provider("SpringDataRestAdapter", function ()
// extract the template parameters of the raw URL
return extractUrl(rawUrl, data[config.linksKey][resourceName].templated);
}

/**
* Returns true if the resource name has URL templates
* @param resourceName the resource name to parse
* @returns {boolean} true if the resource name has URL templates, false otherwise
*/
function hasUrlTemplate(resourceName) {
return typeof resourceName == "string" && resourceName.indexOf("/") > 0;
}

/**
* Extracts the URL template and returns the resource name and the URL templates as an array.
* @param resourceName the resource name to parse
* @returns {[]} the first element is the raw resource name and the second is the extracted URL templates
*/
function extractUrlTemplates(resourceName) {
if(hasUrlTemplate(resourceName)) {
var indexOfSlash = resourceName.indexOf("/");
return [resourceName.substr(0, indexOfSlash), resourceName.substr(indexOfSlash, resourceName.length)];
}
}
};

// return an object with the processData function
Expand Down
2 changes: 1 addition & 1 deletion dist/angular-spring-data-rest.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 32 additions & 2 deletions src/angular-spring-data-rest-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,15 @@ angular.module("spring-data-rest").provider("SpringDataRestAdapter", function ()
var resources = this[config.linksKey];
var parameters = paramDefaults;

var urlTemplates = "";

// split the resourceObject to extract the URL templates for the $resource method
if(hasUrlTemplate(resourceObject)) {
var extractedUrlTemplates = extractUrlTemplates(resourceObject);
resourceObject = extractedUrlTemplates[0];
urlTemplates = extractedUrlTemplates[1];
}

// if a resource object is given process it
if (angular.isObject(resourceObject)) {
if (!resourceObject.name) {
Expand Down Expand Up @@ -181,8 +190,8 @@ angular.module("spring-data-rest").provider("SpringDataRestAdapter", function ()
return resourcesFunction(getProcessedUrl(data, resourceObject.name), parameters, actions, options);
} else if (resourceObject in resources) {

// process the url and call the resources function with the given parameters
return resourcesFunction(getProcessedUrl(data, resourceObject), parameters, actions, options);
// process the url, add the url templates and call the resources function with the given parameters
return resourcesFunction(getProcessedUrl(data, resourceObject) + urlTemplates, parameters, actions, options);
}

// return the available resources as resource object array if the resource object parameter is not set
Expand Down Expand Up @@ -313,6 +322,27 @@ angular.module("spring-data-rest").provider("SpringDataRestAdapter", function ()
// extract the template parameters of the raw URL
return extractUrl(rawUrl, data[config.linksKey][resourceName].templated);
}

/**
* Returns true if the resource name has URL templates
* @param resourceName the resource name to parse
* @returns {boolean} true if the resource name has URL templates, false otherwise
*/
function hasUrlTemplate(resourceName) {
return typeof resourceName == "string" && resourceName.indexOf("/") > 0;
}

/**
* Extracts the URL template and returns the resource name and the URL templates as an array.
* @param resourceName the resource name to parse
* @returns {[]} the first element is the raw resource name and the second is the extracted URL templates
*/
function extractUrlTemplates(resourceName) {
if(hasUrlTemplate(resourceName)) {
var indexOfSlash = resourceName.indexOf("/");
return [resourceName.substr(0, indexOfSlash), resourceName.substr(indexOfSlash, resourceName.length)];
}
}
};

// return an object with the processData function
Expand Down
26 changes: 26 additions & 0 deletions test/angular-spring-data-rest-provider.resources.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -440,5 +440,31 @@ describe("the resources property", function () {
this.rootScope.$apply();
});

it("it must extract the URL templates and add it as suffix to the proper URL", function () {
var resourcesKey = this.config.resourcesKey;
var url = undefined;
var resourcesFunctionConfiguration = {
'resourcesFunction': function (inUrl) {
url = inUrl;
return 'foo';
}
};

// define the resource name and the correct resource href url
var resourceObject = "self/:id";
var resourceHref = "http://localhost:8080/categories";

// set the new resource function with the given parameters
springDataRestAdapterProvider.config(resourcesFunctionConfiguration);
SpringDataRestAdapter.process(this.rawResponse).then(function (processedData) {
// call the new resource method and expect the response and the call to the method
var resourceResponse = processedData[resourcesKey](resourceObject);
expect(resourceResponse).toEqual('foo');
expect(url).toEqual(resourceHref + '/:id');
});

this.rootScope.$apply();
});

});

0 comments on commit 1a22096

Please sign in to comment.