Skip to content

Commit

Permalink
feat: Return promises from resource methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Dirk Grappendorf committed Jul 7, 2015
1 parent a1e5231 commit f0dc329
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 68 deletions.
40 changes: 29 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,35 +108,53 @@ Events
Resource Object methods
-----------------------

In the following 'response object' means

```
{
data: <The response data>,
status: <The response status code>
}
```

* **index(successCallback, errorCallback)**

Performs a HTTP GET on the index URL and returns the response data.
Performs a HTTP GET on the index URL and returns a promise that is resolved with a response object.


* **show(id, successCallback, errorCallback)**

Performs a HTTP GET on the show URL with the specified resource id and returns the response
data.
Performs a HTTP GET on the show URL with the specified resource id and returns a promise that is
resolved with a response object.


* **new(successCallback, errorCallback)**

Performs a HTTP GET on the new URL and returns the response data.
Performs a HTTP GET on the new URL and returns a promise that is resolved with the a response object.


* **create(data, successCallback, errorCallback)**

Performs a HTTP POST on the create URL with the specified resource data and returns
the response data.
Performs a HTTP POST on the create URL with the specified resource data and returns a promise that
is resolved with a response object.


* **update(id, data, successCallback, errorCallback)**

Performs a HTTP PUT on the update URL with the specified resource id and data and returns
the response data.
Performs a HTTP PUT on the update URL with the specified resource id and data and returns a promise
that is resolved with a response object.


* **destroy(id, successCallback, errorCallback)**

Performs a HTTP DELETE on the destroy URL with the specified resource id and returns the response
data.
Performs a HTTP DELETE on the destroy URL with the specified resource id and returns a promise that
is resolved with a response object.


* **memberAction(id, action, successCallback, errorCallback)**

Performs a HTTP PUT on the member URL with "/action" appended to it and the specified resource
id and returns the response data.
id and returns a promise that is resolved with a response object.


In case of an HTTP error the promise is rejected with a response object.
74 changes: 49 additions & 25 deletions grapp-rest-resource.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,39 +67,53 @@
var self;
self = this;
return this.resource = {
index: function(success, error) {
return self._sendRequest('GET', self.indexUrl).then(function(request) {
return self._handleResponse(request.response, request.xhr.status, success, error);
index: function() {
return new Promise(function(resolve, reject) {
return self._sendRequest('GET', self.indexUrl).then(function(request) {
return self._handleResponse(request.response, request.xhr.status, resolve, reject);
});
});
},
show: function(id, success, error) {
return self._sendRequest('GET', self.showUrl, id).then(function(request) {
return self._handleResponse(request.response, request.xhr.status, success, error);
show: function(id) {
return new Promise(function(resolve, reject) {
return self._sendRequest('GET', self.showUrl, id).then(function(request) {
return self._handleResponse(request.response, request.xhr.status, resolve, reject);
});
});
},
"new": function(success, error) {
return self._sendRequest('GET', self.newUrl, null, 'new').then(function(request) {
return self._handleResponse(request.response, request.xhr.status, success, error);
"new": function() {
return new Promise(function(resolve, reject) {
return self._sendRequest('GET', self.newUrl, null, 'new').then(function(request) {
return self._handleResponse(request.response, request.xhr.status, resolve, reject);
});
});
},
create: function(data, success, error) {
return self._sendRequest('POST', self.createUrl, null, data).then(function(request) {
return self._handleResponse(request.response, request.xhr.status, success, error);
create: function(data) {
return new Promise(function(resolve, reject) {
return self._sendRequest('POST', self.createUrl, null, data).then(function(request) {
return self._handleResponse(request.response, request.xhr.status, resolve, reject);
});
});
},
update: function(id, data, success, error) {
return self._sendRequest('PUT', self.updateUrl, id, data).then(function(request) {
return self._handleResponse(request.response, request.xhr.status, success, error);
update: function(id, data) {
return new Promise(function(resolve, reject) {
return self._sendRequest('PUT', self.updateUrl, id, data).then(function(request) {
return self._handleResponse(request.response, request.xhr.status, resolve, reject);
});
});
},
destroy: function(id, success, error) {
return self._sendRequest('DELETE', self.destroyUrl, id).then(function(request) {
return self._handleResponse(request.response, request.xhr.status, success, error);
destroy: function(id) {
return new Promise(function(resolve, reject) {
return self._sendRequest('DELETE', self.destroyUrl, id).then(function(request) {
return self._handleResponse(request.response, request.xhr.status, resolve, reject);
});
});
},
memberAction: function(id, action, success, error) {
return self._sendRequest('PUT', self.memberUrl, id, action).then(function(request) {
return self._handleResponse(request.response, request.xhr.status, success, error);
memberAction: function(id, action) {
return new Promise(function(resolve, reject) {
return self._sendRequest('PUT', self.memberUrl, id, action).then(function(request) {
return self._handleResponse(request.response, request.xhr.status, resolve, reject);
});
});
}
};
Expand Down Expand Up @@ -154,15 +168,25 @@
body: (data ? JSON.stringify(data) : void 0)
});
},
_handleResponse: function(response, status, success, error) {
_handleResponse: function(response, status, resolve, reject) {
var json;
json = (response && response.trim() !== '' ? JSON.parse(response) : {});
if (status >= 200 && status <= 299) {
return typeof success === "function" ? success(json) : void 0;
return resolve({
data: json,
status: status
});
} else if (status === 401) {
return self.fire('grapp-authentication-error');
self.fire('grapp-authentication-error');
return reject({
data: json,
status: status
});
} else {
return typeof error === "function" ? error(json) : void 0;
return reject({
data: json,
status: status
});
}
}
});
Expand Down
10 changes: 6 additions & 4 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ <h2>Demo</h2>

<grapp-rest-resource id="data" url="test/example.json"></grapp-rest-resource>

<template is="dom-repeat" items="[[data]]">
<div>Response code: <span>[[status]]</span></div>
<template is="dom-repeat" items="[[items]]">
<p>
<span>{{item.name}}</span>: <span>{{item.role}}</span>
</p>
Expand All @@ -44,9 +45,10 @@ <h2>Demo</h2>
Polymer({
is: 'demo-element',
ready: function() {
this.$.data.resource.index((function(result) {
this.data = result;
}).bind(this));
this.$.data.resource.index().then(function(response) {
this.items = response.data;
this.status = response.status;
}.bind(this));
}
});
</script>
Expand Down
56 changes: 32 additions & 24 deletions src/grapp-rest-resource.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,40 @@ Polymer
self = @

@resource =
index: (success, error) ->
self._sendRequest('GET', self.indexUrl).then (request) ->
self._handleResponse request.response, request.xhr.status, success, error
index: ->
new Promise (resolve, reject) ->
self._sendRequest('GET', self.indexUrl).then (request) ->
self._handleResponse request.response, request.xhr.status, resolve, reject

show: (id, success, error) ->
self._sendRequest('GET', self.showUrl, id).then (request) ->
self._handleResponse request.response, request.xhr.status, success, error
show: (id) ->
new Promise (resolve, reject) ->
self._sendRequest('GET', self.showUrl, id).then (request) ->
self._handleResponse request.response, request.xhr.status, resolve, reject

new: (success, error) ->
self._sendRequest('GET', self.newUrl, null, 'new').then (request) ->
self._handleResponse request.response, request.xhr.status, success, error
new: () ->
new Promise (resolve, reject) ->
self._sendRequest('GET', self.newUrl, null, 'new').then (request) ->
self._handleResponse request.response, request.xhr.status, resolve, reject

create: (data, success, error) ->
self._sendRequest('POST', self.createUrl, null, data).then (request) ->
self._handleResponse request.response, request.xhr.status, success, error
create: (data) ->
new Promise (resolve, reject) ->
self._sendRequest('POST', self.createUrl, null, data).then (request) ->
self._handleResponse request.response, request.xhr.status, resolve, reject

update: (id, data, success, error) ->
self._sendRequest('PUT', self.updateUrl, id, data).then (request) ->
self._handleResponse request.response, request.xhr.status, success, error
update: (id, data) ->
new Promise (resolve, reject) ->
self._sendRequest('PUT', self.updateUrl, id, data).then (request) ->
self._handleResponse request.response, request.xhr.status, resolve, reject

destroy: (id, success, error) ->
self._sendRequest('DELETE', self.destroyUrl, id).then (request) ->
self._handleResponse request.response, request.xhr.status, success, error
destroy: (id) ->
new Promise (resolve, reject) ->
self._sendRequest('DELETE', self.destroyUrl, id).then (request) ->
self._handleResponse request.response, request.xhr.status, resolve, reject

memberAction: (id, action, success, error) ->
self._sendRequest('PUT', self.memberUrl, id, action).then (request) ->
self._handleResponse request.response, request.xhr.status, success, error
memberAction: (id, action) ->
new Promise (resolve, reject) ->
self._sendRequest('PUT', self.memberUrl, id, action).then (request) ->
self._handleResponse request.response, request.xhr.status, resolve, reject

_prepareUrl: (url, params, id, action) ->
params = JSON.parse(params) if typeof(params) == 'string'
Expand Down Expand Up @@ -74,11 +81,12 @@ Polymer
headers: @_prepareHeaders()
body: (if data then JSON.stringify data else undefined)

_handleResponse: (response, status, success, error) ->
_handleResponse: (response, status, resolve, reject) ->
json = (if response && response.trim() != '' then JSON.parse response else {})
if status >= 200 && status <= 299
success? json
resolve data: json, status: status
else if status == 401
self.fire 'grapp-authentication-error'
reject data: json, status: status
else
error? json
reject data: json, status: status
8 changes: 4 additions & 4 deletions test/basic.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@

it 'retrieves all items', (done) ->
element = document.querySelector '#element'
element.resource.index (result) ->
expect(result).to.eql [1, 2, 3]
element.resource.index().then (response) ->
expect(response.data).to.eql [1, 2, 3]
done()

describe 'when a special URL is defined for a method', ->

it 'accesses the special URL', (done) ->
element = document.querySelector '#element_with_special_url'
element.resource.index (result) ->
expect(result).to.eql [42]
element.resource.index().then (response) ->
expect(response.data).to.eql [42]
done()

</script>
Expand Down

0 comments on commit f0dc329

Please sign in to comment.