|
1 | 1 | # angular-spring-data-rest |
2 | 2 |
|
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. |
4 | 4 |
|
5 | 5 | #Table of contents |
6 | 6 |
|
|
9 | 9 | - [Usage of `SpringDataRestAdapter`](#usage-of-springdatarestadapter) |
10 | 10 | - [Usage of `_resources` method](#usage-of-_resources-method) |
11 | 11 | - [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) |
13 | 14 | - [Usage of `_embeddedItems` property](#usage-of-_embeddeditems-property) |
14 | 15 | - [`_embeddedItems` usage example](#_embeddeditems-usage-example) |
15 | 16 | - [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 |
81 | 82 |
|
82 | 83 | 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: |
83 | 84 |
|
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). |
85 | 86 | 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). |
86 | 87 |
|
87 | 88 | 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 = { |
169 | 170 | processedResponse._resources(resourceObject, paramDefaults, actions, options); |
170 | 171 | ``` |
171 | 172 |
|
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. |
173 | 174 |
|
174 | 175 | * `paramDefaults`: the default values for url parameters. Read more [here](https://docs.angularjs.org/api/ngResource/service/$resource). |
175 | 176 | * `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() { |
238 | 239 | }); |
239 | 240 | ``` |
240 | 241 |
|
| 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 | + |
241 | 262 | ### Usage of `_embeddedItems` property |
242 | 263 |
|
243 | 264 | 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 |
267 | 288 | * `embedded.value` (default: `_embeddedItems`): the property name where the array of embedded items are stored. |
268 | 289 | * `hrefKey` (default: `href`): the property name where the url is stored under each specific link. |
269 | 290 | * `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) |
270 | 292 |
|
271 | 293 | You are able to configure the `SpringDataRestAdapter` provider in a *Angular* configuration block in the following way: |
272 | 294 |
|
@@ -294,11 +316,11 @@ The config method of the `SpringDataRestAdapterProvider` takes a configuration o |
294 | 316 | "value': "_embeddedItems" |
295 | 317 | }, |
296 | 318 | "hrefKey": "href", |
297 | | - "resourcesKey": "_resources" |
| 319 | + "resourcesKey": "_resources", |
| 320 | + "resourcesFunction": undefined |
298 | 321 | } |
299 | 322 | ``` |
300 | 323 |
|
301 | | - |
302 | 324 | ## The `SpringDataRestInterceptor` |
303 | 325 |
|
304 | 326 | 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: |
|
0 commit comments