Skip to content

Commit

Permalink
chore(README): adds information about nested properties
Browse files Browse the repository at this point in the history
Closes #229
  • Loading branch information
iobaixas committed May 7, 2015
1 parent 2b201f1 commit c934f1a
Showing 1 changed file with 47 additions and 2 deletions.
49 changes: 47 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -870,7 +870,7 @@ then you use it as a standard decoder like this:

```javascript
var Bike = restmod.model('/bikes').mix({
createdAt: {decode:'date_parse'}
createdAt: {decode: 'date_parse' }
});
```

Expand Down Expand Up @@ -914,7 +914,7 @@ var Bike = restmod.model('/bikes').mix({

### Explicit attribute mapping

You can explicitly tell restmod to map a given server attribute to one of the model's attributs:
You can explicitly tell restmod to map a given server attribute to one of the model's attributes:

```javascript
var Bike = restmod.model('/bikes').mix({
Expand All @@ -932,6 +932,51 @@ var User = restmod.model('/users').mix({
});
```

### Nested properties

Sometimes you will need to specify behaviour for nested properties, this is done the same ways as with regular properties using the `.` symbol.

Given the following json response:

```json
{
"id": 1,
"serialNo": {
"issued": '2014-05-05'
}
}
```

You can add a date decoder for the `issued` property using:

```javascript
var Bike = restmod.model('/bikes').mix({
'serialNo.issued': { decode: 'date_parse' }
});
```

If the nested property is inside an array, you can reffer to it using the `[]` symbols.

So if the json response looks like this:

```json
{
"id": 1,
"tags": [
{ name: 'endurow', weight: 20 },
{ name: 'offroad', weight: 5 }
]
}
```

You can add a mapping for the `weight` property to the `size` property using:

```javascript
var Bike = restmod.model('/bikes').mix({
'tags[].size': { map: 'weight' }
});
```

## Custom methods

A restmod object is composed of three main APIs, the Model static API, the record API and the collection API.
Expand Down

0 comments on commit c934f1a

Please sign in to comment.