Skip to content

Commit 0d71bce

Browse files
author
guylabs
committed
Add ability to exchange underlying call to backend. Restructure source and test files. Update the package.json and bower.json. Update the grunt and the karma configs.
1 parent 6c51db4 commit 0d71bce

11 files changed

+201
-48
lines changed

Gruntfile.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ module.exports = function (grunt) {
44
pkg: grunt.file.readJSON("package.json"),
55
concat: {
66
options: {
7-
separator: '\n'
7+
separator: '\n',
8+
process: true
89
},
910
dist: {
1011
src: [
@@ -13,7 +14,7 @@ module.exports = function (grunt) {
1314
'src/angular-spring-data-rest-interceptor-provider.js',
1415
'src/angular-spring-data-rest-utils.js',
1516
],
16-
dest: 'dist/<%= pkg.name %>.<%= pkg.version %>.js'
17+
dest: 'dist/<%= pkg.name %>.js'
1718
}
1819
},
1920
uglify: {
@@ -22,7 +23,7 @@ module.exports = function (grunt) {
2223
},
2324
dist: {
2425
files: {
25-
'dist/<%= pkg.name %>.<%= pkg.version %>.min.js': ['<%= concat.dist.dest %>']
26+
'dist/<%= pkg.name %>.min.js': ['<%= concat.dist.dest %>']
2627
}
2728
}
2829
},

README.md

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# angular-spring-data-rest
22

3-
> An AngularJS module with an additional interceptor which wraps the Angular [$resource](https://docs.angularjs.org/api/ngResource/service/$resource) function and therefore eases the use with a [Spring Data REST](http://projects.spring.io/spring-data-rest) backend.
3+
> An AngularJS module with an additional interceptor which eases the work with a [Spring Data REST](http://projects.spring.io/spring-data-rest) backend.
44
55
#Table of contents
66

@@ -9,7 +9,8 @@
99
- [Usage of `SpringDataRestAdapter`](#usage-of-springdatarestadapter)
1010
- [Usage of `_resources` method](#usage-of-_resources-method)
1111
- [The `_resources` method parameters and return type](#the-_resources-method-parameters-and-return-type)
12-
- [`_resources` usage example](#_resources-usage-example)
12+
- [`_resources` usage example](#_resources-usage-example)
13+
- [Exchange the underlying Angular `$resource` function](#exchange-the-underlying-angular-$resource-function)
1314
- [Usage of `_embeddedItems` property](#usage-of-_embeddeditems-property)
1415
- [`_embeddedItems` usage example](#_embeddeditems-usage-example)
1516
- [Configuration of the `SpringDataRestAdapter`](#configuration-of-the-springdatarestadapter)
@@ -81,7 +82,7 @@ This *Angular* module provides two ways of processing a response from the *Sprin
8182

8283
The `spring-data-rest` *Angular* module provides a provider for the `SpringDataRestAdapter` object. This object is the core of the module and it processes a given response and adds the following additional properties/methods to it:
8384

84-
1. `_resources`: this method wraps the *Angular* `$resource` function and adds an easy way to retrieve the resources defined in the `_links` property. It is also used to retrieve all available resources of the given object. Read more about this property [here](#usage-of-_resources-property).
85+
1. `_resources`: this method wraps the *Angular* `$resource` function by default (this is [exchangeable](#exchange-the-underlying-angular-$resource-function)) and adds an easy way to retrieve the resources defined in the `_links` property. It is also used to retrieve all available resources of the given object. Read more about this property [here](#usage-of-_resources-property).
8586
2. `_embeddedItems`: this property replaces the `_embedded` property and sets the named array (`categories` in the upper example response) with the embedded items as its value. Read more about this property [here](#usage-of-_embeddedItems-property).
8687

8788
Spring Data REST also generates an index response when you make a `GET` response to the configured base url of the dispatcher servlet. This response looks like the following example:
@@ -169,7 +170,7 @@ var resourceObject = {
169170
processedResponse._resources(resourceObject, paramDefaults, actions, options);
170171
```
171172

172-
This will call *Angular* `$resource` method with the `href` of the `self` resource and will add the parameters `size` and `sort` as query string to the URL. If the resource object parameters and the `paramDefaults` parameters are set, then these two objects are merged such that the resource object parameters appear first in the new object and the `paramDefaults` parameters last.
173+
This will call *Angular* `$resource` method by default (this is [exchangeable](#exchange-the-underlying-angular-$resource-function)) with the `href` of the `self` resource and will add the parameters `size` and `sort` as query string to the URL. If the resource object parameters and the `paramDefaults` parameters are set, then these two objects are merged such that the resource object parameters appear first in the new object and the `paramDefaults` parameters last.
173174

174175
* `paramDefaults`: the default values for url parameters. Read more [here](https://docs.angularjs.org/api/ngResource/service/$resource).
175176
* `actions`: custom action that should extend the default set of the `$resource` actions. Read more [here](https://docs.angularjs.org/api/ngResource/service/$resource).
@@ -238,6 +239,26 @@ var parentCategory = parentCategoryResource.get(function() {
238239
});
239240
```
240241

242+
#### Exchange the underlying Angular `$resource` function
243+
244+
If you want to exchange the underlying call to the *Angular* `$resource` method then you are able to do this within the configuration of the `SpringDataRestAdapter`. By default it will use the *Angular* `$resource` function.
245+
246+
The following example shows how to set a custom function:
247+
248+
```javascript
249+
myApp.config(function (SpringDataRestAdapterProvider) {
250+
251+
// set the new resource function
252+
SpringDataRestAdapterProvider.config({
253+
'resourcesFunction': function (url, paramDefaults, actions, options) {
254+
// do the call to the backend and return your desired object
255+
}
256+
});
257+
});
258+
```
259+
260+
The description of the parameters you will find [here](#the-_resources-method-parameters-and-return-type). You can also read more about the configuration of the `SpringDataRestAdapter` [here](#configuration-of-the-springdatarestadapter)
261+
241262
### Usage of `_embeddedItems` property
242263

243264
The `_embeddedItems` property is just a convention property created by the `SpringDataRestAdapter` to easily iterate over the `_emebedded` items in the response. Like with the `_resources` method, the `SpringDataRestAdapter` will recursively create an `_embeddedItems` property on the same level as a `_embedded` property exists for all the JSON response properties.
@@ -267,6 +288,7 @@ The `SpringDataRestAdapter` is designed to be configurable and you are able to c
267288
* `embedded.value` (default: `_embeddedItems`): the property name where the array of embedded items are stored.
268289
* `hrefKey` (default: `href`): the property name where the url is stored under each specific link.
269290
* `resourcesKey` (default: `_resources`): the property name where the resource method is stored.
291+
* `resourcesFunction` (default: `undefined`): the function to use to call the backend. Read more how to do this [here](#exchange-the-underlying-angular-$resource-function)
270292

271293
You are able to configure the `SpringDataRestAdapter` provider in a *Angular* configuration block in the following way:
272294

@@ -294,11 +316,11 @@ The config method of the `SpringDataRestAdapterProvider` takes a configuration o
294316
"value': "_embeddedItems"
295317
},
296318
"hrefKey": "href",
297-
"resourcesKey": "_resources"
319+
"resourcesKey": "_resources",
320+
"resourcesFunction": undefined
298321
}
299322
```
300323

301-
302324
## The `SpringDataRestInterceptor`
303325

304326
If you want to use the `SpringDataRestAdapter` for all responses of the *Angular* `$http` service then you can add the `SpringDataRestInterceptor` to the `$httpProvider.interceptors` in an *Angular* configuration block:

RELEASENOTES.md

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

3+
## Version 0.2.0
4+
5+
* Tag: [0.2.0](https://github.com/guylabs/angular-spring-data-rest/tree/0.2.0)
6+
* Release: [angular-spring-data-rest-0.2.0.zip](https://github.com/guylabs/angular-spring-data-rest/releases/download/0.2.0/angular-spring-data-rest-0.2.0.zip)
7+
8+
### Changes
9+
10+
* Add ability to exchange the *Angular* ``$resource`` method with an own implementation
11+
* Updated source and test files structure
12+
* Add package to bower repository
13+
314
## Version 0.1.0
415

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

bower.json

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,45 @@
11
{
22
"name": "angular-spring-data-rest",
3-
"version": "0.1.0",
4-
"authors": [
5-
"Guy Brûlé <admin@guylabs.org>"
3+
"description": "An AngularJS module to ease the work with a Spring Data REST backend.",
4+
"main": "dist/angular-spring-data-rest.js",
5+
"license": "MIT",
6+
"ignore": [
7+
"**/.*",
8+
"node_modules",
9+
"bower_components",
10+
"dist",
11+
".idea",
12+
"*.iml"
613
],
7-
"description": "An AngularJS module for using `$resource` with a Spring Data REST backend.",
814
"keywords": [
915
"angular",
10-
"resource",
16+
"angularjs",
1117
"spring",
1218
"spring data",
1319
"spring data rest",
1420
"rest",
1521
"api"
1622
],
17-
"license": "MIT",
18-
"homepage": "https://github.com/guylabs/angular-spring-data-rest",
19-
"main": "src/angular-spring-data-rest.js",
20-
"ignore": [
21-
"**/.*",
22-
"node_modules",
23-
"bower_components",
24-
"dist",
25-
".idea",
26-
"*.iml"
27-
]
23+
"authors": [
24+
"Guy Brûlé <admin@guylabs.org>"
25+
],
26+
"homepage": "http://guylabs.ch/project/angular-spring-data-rest",
27+
"repository": {
28+
"type": "git",
29+
"url": "https://github.com/guylabs/angular-spring-data-rest.git"
30+
},
31+
"devDependencies": {
32+
"grunt": "0.4.5",
33+
"grunt-contrib-uglify": "0.5.1",
34+
"grunt-contrib-concat": "0.5.0",
35+
"grunt-karma": "0.8.3",
36+
"karma": "0.12.22",
37+
"karma-chrome-launcher": "0.1.4",
38+
"karma-coffee-preprocessor": "0.2.1",
39+
"karma-firefox-launcher": "0.1.3",
40+
"karma-jasmine": "0.1.5",
41+
"karma-phantomjs-launcher": "0.1.3",
42+
"karma-requirejs": "0.2.2",
43+
"requirejs": "2.1.14"
44+
}
2845
}

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
22
"name": "angular-spring-data-rest",
33
"version": "0.1.0",
4-
"description": "An AngularJS module for a Spring Data REST backend.",
4+
"description": "An AngularJS module to ease the work with a Spring Data REST backend.",
55
"keywords": ["AngularJS", "angular", "Spring", "Spring Data", "REST", "Spring Data REST"],
6-
"homepage": "https://github.com/guylabs/angular-spring-data-rest",
6+
"homepage": "http://guylabs.ch/project/angular-spring-data-rest",
77
"bugs": "https://github.com/guylabs/angular-spring-data-rest/issues",
88
"author": {
99
"name": "Guy Brûlé",
10-
"email": "adming@guylabs.org",
10+
"email": "admin@guylabs.org",
1111
"url": "http://guylabs.ch"
1212
},
1313
"repository": {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @module spring-data-rest
3-
* @version 0.1.0
3+
* @version <%= pkg.version %>
44
*
55
* Provider for the interceptor which wraps the SpringDataRestAdapter around the response object.
66
*/
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @module spring-data-rest
3-
* @version 0.1.0
3+
* @version <%= pkg.version %>
44
*
5-
* An AngularJS module for using `$resource` with a Spring Data REST backend.
5+
* An AngularJS module to ease the work with a Spring Data REST backend.
66
*/
77
angular.module("spring-data-rest", ["ngResource"]);

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

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @module spring-data-rest
3-
* @version 0.1.0
3+
* @version <%= pkg.version %>
44
*
55
* Provider for the SpringDataRestAdapter which is the core of this module.
66
*/
@@ -18,7 +18,8 @@ angular.module("spring-data-rest").provider("SpringDataRestAdapter", function ()
1818
'value': '_embeddedItems'
1919
},
2020
'hrefKey': 'href',
21-
'resourcesKey': '_resources'
21+
'resourcesKey': '_resources',
22+
'resourcesFunction': undefined
2223
};
2324

2425
return {
@@ -34,8 +35,14 @@ angular.module("spring-data-rest").provider("SpringDataRestAdapter", function ()
3435
if (typeof newConfig !== 'undefined') {
3536
// throw an error if the given configuration is not an object
3637
if (!angular.isObject(newConfig)) {
37-
throw new Error("The given configuration " + newConfig + " is not an object.");
38+
throw new Error("The given configuration '" + newConfig + "' is not an object.");
3839
}
40+
41+
// check if the given resource function is not undefined and is of type function
42+
if (newConfig.resourcesFunction != undefined && typeof(newConfig.resourcesFunction) != "function") {
43+
throw new Error("The given resource function '" + newConfig.resourcesFunction + "' is not of type function.")
44+
}
45+
3946
// override the default configuration properties with the given new configuration
4047
config = deepExtend(config, newConfig);
4148
}
@@ -54,7 +61,11 @@ angular.module("spring-data-rest").provider("SpringDataRestAdapter", function ()
5461
* @returns {*}
5562
*/
5663
function callBackend(url, paramDefaults, actions, options) {
57-
return $injector.get("$resource")(url, paramDefaults, actions, options);
64+
if (config.resourcesFunction == undefined) {
65+
return $injector.get("$resource")(url, paramDefaults, actions, options);
66+
} else {
67+
return config.resourcesFunction(url, paramDefaults, actions, options);
68+
}
5869
}
5970

6071
/**
@@ -88,7 +99,7 @@ angular.module("spring-data-rest").provider("SpringDataRestAdapter", function ()
8899
throw new Error("The provided resource object must contain a name property.");
89100
}
90101

91-
var parameters = undefined;
102+
var parameters = paramDefaults;
92103
var resourceObjectParameters = resourceObject.parameters;
93104

94105
// if the default parameters and the resource object parameters are objects, then merge these two objects
@@ -105,12 +116,12 @@ angular.module("spring-data-rest").provider("SpringDataRestAdapter", function ()
105116
}
106117
}
107118

108-
return callBackend(extractUrl(this, data[config.links.key][resourceObject.name][config.hrefKey],
119+
return callBackend(extractUrl(data[config.links.key][resourceObject.name][config.hrefKey],
109120
data[config.links.key][resourceObject.name].templated), parameters, actions, options);
110121

111122
} else if (resourceObject in resources) {
112123
// get the url out of the resource name and return the backend function
113-
return callBackend(extractUrl(this, data[config.links.key][resourceObject][config.hrefKey],
124+
return callBackend(extractUrl(data[config.links.key][resourceObject][config.hrefKey],
114125
data[config.links.key][resourceObject].templated), paramDefaults, actions, options);
115126
}
116127

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ var moveArray = function (object, sourceKey, destinationKey) {
4141
};
4242

4343
/**
44-
* Extracts the url out of a resource object. If template parameters exist, they will be removed from the
44+
* Extracts the url out of a url string. If template parameters exist, they will be removed from the
4545
* returned url.
4646
*
47-
* @param {object} data the data object to get the resource object from
48-
* @param {string} resourceName the name of the resource
47+
* @param {string} url the url string from which to extract the url
48+
* @param {boolean} templated true if the url is templated
4949
* @returns {string} the url of the resource object
5050
*/
51-
function extractUrl(data, url, templated) {
51+
function extractUrl(url, templated) {
5252
if (templated) {
5353
url = removeTemplateParameters(url)
5454
}

0 commit comments

Comments
 (0)