Skip to content
This repository has been archived by the owner on May 4, 2019. It is now read-only.

Commit

Permalink
Merge pull request #5 from marmelab/no_closure
Browse files Browse the repository at this point in the history
[RFR] Switch to a method-based exploration of entities
  • Loading branch information
RobinBressan committed Mar 10, 2015
2 parents 2c71ac1 + 9757235 commit 0e33f7d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 14 deletions.
34 changes: 22 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,19 @@ var commentsCollection = articleMember.all('comments'); // http://api.example.c

#### Entities

Once you have collections and members endpoints, fetch them to get *entities*. Restful.js exposes `get()` and `getAll()` methods for fetching endpoints. Since these methods are asynchronous, they return a Promise ([based on the ES6 Promise specification](https://github.com/jakearchibald/es6-promise)).
Once you have collections and members endpoints, fetch them to get *entities*. Restful.js exposes `get()` and `getAll()` methods for fetching endpoints. Since these methods are asynchronous, they return a Promise ([based on the ES6 Promise specification](https://github.com/jakearchibald/es6-promise)) for an entity or an array of entities.

```javascript
```js
var articleMember = api.one('articles', 1); // http://api.example.com/articles/1
articleMember.get().then(function(articleEntity) {
var article = articleEntity().data;
var article = articleEntity.data();
console.log(article.title); // hello, world!
});

var commentsCollection = articleMember.all('comments'); // http://api.example.com/articles/1/comments
commentsCollection.getAll().then(function(commentEntities) {
commentEntities.forEach(function(commentEntity) {
var comment = commentEntity().data;
var comment = commentEntity.data();
console.log(comment.body);
})
});
Expand All @@ -105,17 +105,20 @@ commentsCollection.get(4).then(function(commentEntity) {
});
```

### Entity Methods
### Entity Data

An en entity is a closure that you can execute to get the response fetched from the endpoint. The entity is mapped under the `data` key.
An en entity is made from the HTTP response fetched from the endpoint. It exposes `status()`, `headers()`, and `data()` methods:

```javascript
```js
var articleCollection = api.all('articles'); // http://api.example.com/articles

// http://api.example.com/articles/1
api.one('articles', 1).get().then(function(articleEntity) {
if (articleEntity.status() !== 200) {
throw new Error('Invalid response');
}
// if the server response was { id: 1, title: 'test', body: 'hello' }
var article = articleEntity().data;
var article = articleEntity.data();
article.title; // returns `test`
article.body; // returns `hello`
// You can also edit it
Expand All @@ -126,7 +129,7 @@ api.one('articles', 1).get().then(function(articleEntity) {
});
```

You can also use the entity to continue exploring the API. Entities expose several methods to chain calls:
You can also use the entity to continue exploring the API. Entities expose several other methods to chain calls:

* `entity.one(name, id)`: Query a member child of the entity.
* `entity.all(name)`: Query a collection child of the entity.
Expand All @@ -135,7 +138,7 @@ You can also use the entity to continue exploring the API. Entities expose sever
* `entity.remove()`: Remove the entity by performing a DELETE request.
* `entity.id()`: Get the id of the entity.

```javascript
```js
var articleMember = api.one('articles', 1); // http://api.example.com/articles/1
var commentMember = articleMember.one('comments', 3); // http://api.example.com/articles/1/comments/3
commentMember.get()
Expand All @@ -144,15 +147,15 @@ commentMember.get()
return comment.all('authors').getAll(); // http://api.example.com/articles/1/comments/3/authors
}).then(function(authorEntities) {
authorEntities.forEach(function(authorEntity) {
var author = authorEntity().data;
var author = authorEntity.data();
console.log(author.name);
});
});
```

Restful.js uses an inheritance pattern when collections or members are chained. That means that when you configure a collection or a member, it will configure all the collection an members chained afterwards.

```javascript
```js
// configure the api
api.header('AuthToken', 'test');

Expand Down Expand Up @@ -224,6 +227,13 @@ resource.addRequestInterceptor(function(data, headers, method, url) {

### Entity methods

* `entity.status()`: Get the HTTP statuis code of the response
* `entity.headers()`: Get the HTTP headers of the response
* `entity.data()` : Get the JS object unserialized from the response body (which must be in JSON)
* `entity.one(name, id)`: Query a member child of the entity.
* `entity.all(name)`: Query a collection child of the entity.
* `entity.url()`: Get the entity url.
* `entity.id()`: Get the id of the entity.
* `entity.save ( [ headers ] )`: Update the member link to the entity. Returns a promise with the response.
* `entity.remove ( [ headers ] )`: Delete the member link to the entity. Returns a promise with the response.

Expand Down
Loading

0 comments on commit 0e33f7d

Please sign in to comment.