From 6ce1a9f7718dc28737b4f92858b03b10661610de Mon Sep 17 00:00:00 2001 From: Kaloyan Kosev Date: Tue, 26 Sep 2017 19:06:23 +0300 Subject: [PATCH] Add: example with more complex (nested) response --- README.md | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2d53e25..d4bf97d 100644 --- a/README.md +++ b/README.md @@ -177,7 +177,9 @@ Type: `Object` or `Array` The mapping of the attribute names you'd like to use across your codebase with the attribute names coming from the server response. -- In case the server returns a single Object, you can use the following syntax: +- In case the server response is a single Object or an Array of Objects: + + - Syntax 1: In case the server returns a single Object, you can use the following syntax: ```javascript const WeatherRepository = new SuperRepo({ @@ -190,7 +192,7 @@ The mapping of the attribute names you'd like to use across your codebase with t }); ``` -- In case the server returns an Array of items, you can use the following syntax: + - Syntax 2: In case the server returns an Array of items, you can use the following syntax: ```javascript const WeatherRepository = new SuperRepo({ @@ -203,6 +205,41 @@ The mapping of the attribute names you'd like to use across your codebase with t }); ``` +- In case the server response is more complex or nested a bit more, use [`mapData`](https://github.com/superKalo/super-repo#mapdata-optional) to apply your data model manually. Here's an example. + Let's say our Weather API response is: + + ```json + { + "current": { + "main": { + "t": 31, + "w": 32, + "h": 38 + }, + "additional": { + "f": 32, + "p": 1011 + } + } + } + ``` + + Here's how we handle this case: + + ```javascript + const WeatherRepository = new SuperRepo({ + /* ... */ + mapData( data => { + return { + temperature: data.current.main.t, + windspeed: data.current.main.w, + pressure: data.current.additional.p + } + }); + // ... and simply don't pass `dataModel` + }); + ``` + #### `outOfDateAfter` [optional] Default: `-1` | Type: `Number`, in milliseconds