Skip to content

Commit f0dc329

Browse files
author
Dirk Grappendorf
committed
feat: Return promises from resource methods
1 parent a1e5231 commit f0dc329

File tree

5 files changed

+120
-68
lines changed

5 files changed

+120
-68
lines changed

README.md

+29-11
Original file line numberDiff line numberDiff line change
@@ -108,35 +108,53 @@ Events
108108
Resource Object methods
109109
-----------------------
110110

111+
In the following 'response object' means
112+
113+
```
114+
{
115+
data: <The response data>,
116+
status: <The response status code>
117+
}
118+
```
119+
111120
* **index(successCallback, errorCallback)**
112121

113-
Performs a HTTP GET on the index URL and returns the response data.
122+
Performs a HTTP GET on the index URL and returns a promise that is resolved with a response object.
123+
114124

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

117-
Performs a HTTP GET on the show URL with the specified resource id and returns the response
118-
data.
127+
Performs a HTTP GET on the show URL with the specified resource id and returns a promise that is
128+
resolved with a response object.
129+
119130

120131
* **new(successCallback, errorCallback)**
121132

122-
Performs a HTTP GET on the new URL and returns the response data.
133+
Performs a HTTP GET on the new URL and returns a promise that is resolved with the a response object.
134+
123135

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

126-
Performs a HTTP POST on the create URL with the specified resource data and returns
127-
the response data.
138+
Performs a HTTP POST on the create URL with the specified resource data and returns a promise that
139+
is resolved with a response object.
140+
128141

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

131-
Performs a HTTP PUT on the update URL with the specified resource id and data and returns
132-
the response data.
144+
Performs a HTTP PUT on the update URL with the specified resource id and data and returns a promise
145+
that is resolved with a response object.
146+
133147

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

136-
Performs a HTTP DELETE on the destroy URL with the specified resource id and returns the response
137-
data.
150+
Performs a HTTP DELETE on the destroy URL with the specified resource id and returns a promise that
151+
is resolved with a response object.
152+
138153

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

141156
Performs a HTTP PUT on the member URL with "/action" appended to it and the specified resource
142-
id and returns the response data.
157+
id and returns a promise that is resolved with a response object.
158+
159+
160+
In case of an HTTP error the promise is rejected with a response object.

grapp-rest-resource.html

+49-25
Original file line numberDiff line numberDiff line change
@@ -67,39 +67,53 @@
6767
var self;
6868
self = this;
6969
return this.resource = {
70-
index: function(success, error) {
71-
return self._sendRequest('GET', self.indexUrl).then(function(request) {
72-
return self._handleResponse(request.response, request.xhr.status, success, error);
70+
index: function() {
71+
return new Promise(function(resolve, reject) {
72+
return self._sendRequest('GET', self.indexUrl).then(function(request) {
73+
return self._handleResponse(request.response, request.xhr.status, resolve, reject);
74+
});
7375
});
7476
},
75-
show: function(id, success, error) {
76-
return self._sendRequest('GET', self.showUrl, id).then(function(request) {
77-
return self._handleResponse(request.response, request.xhr.status, success, error);
77+
show: function(id) {
78+
return new Promise(function(resolve, reject) {
79+
return self._sendRequest('GET', self.showUrl, id).then(function(request) {
80+
return self._handleResponse(request.response, request.xhr.status, resolve, reject);
81+
});
7882
});
7983
},
80-
"new": function(success, error) {
81-
return self._sendRequest('GET', self.newUrl, null, 'new').then(function(request) {
82-
return self._handleResponse(request.response, request.xhr.status, success, error);
84+
"new": function() {
85+
return new Promise(function(resolve, reject) {
86+
return self._sendRequest('GET', self.newUrl, null, 'new').then(function(request) {
87+
return self._handleResponse(request.response, request.xhr.status, resolve, reject);
88+
});
8389
});
8490
},
85-
create: function(data, success, error) {
86-
return self._sendRequest('POST', self.createUrl, null, data).then(function(request) {
87-
return self._handleResponse(request.response, request.xhr.status, success, error);
91+
create: function(data) {
92+
return new Promise(function(resolve, reject) {
93+
return self._sendRequest('POST', self.createUrl, null, data).then(function(request) {
94+
return self._handleResponse(request.response, request.xhr.status, resolve, reject);
95+
});
8896
});
8997
},
90-
update: function(id, data, success, error) {
91-
return self._sendRequest('PUT', self.updateUrl, id, data).then(function(request) {
92-
return self._handleResponse(request.response, request.xhr.status, success, error);
98+
update: function(id, data) {
99+
return new Promise(function(resolve, reject) {
100+
return self._sendRequest('PUT', self.updateUrl, id, data).then(function(request) {
101+
return self._handleResponse(request.response, request.xhr.status, resolve, reject);
102+
});
93103
});
94104
},
95-
destroy: function(id, success, error) {
96-
return self._sendRequest('DELETE', self.destroyUrl, id).then(function(request) {
97-
return self._handleResponse(request.response, request.xhr.status, success, error);
105+
destroy: function(id) {
106+
return new Promise(function(resolve, reject) {
107+
return self._sendRequest('DELETE', self.destroyUrl, id).then(function(request) {
108+
return self._handleResponse(request.response, request.xhr.status, resolve, reject);
109+
});
98110
});
99111
},
100-
memberAction: function(id, action, success, error) {
101-
return self._sendRequest('PUT', self.memberUrl, id, action).then(function(request) {
102-
return self._handleResponse(request.response, request.xhr.status, success, error);
112+
memberAction: function(id, action) {
113+
return new Promise(function(resolve, reject) {
114+
return self._sendRequest('PUT', self.memberUrl, id, action).then(function(request) {
115+
return self._handleResponse(request.response, request.xhr.status, resolve, reject);
116+
});
103117
});
104118
}
105119
};
@@ -154,15 +168,25 @@
154168
body: (data ? JSON.stringify(data) : void 0)
155169
});
156170
},
157-
_handleResponse: function(response, status, success, error) {
171+
_handleResponse: function(response, status, resolve, reject) {
158172
var json;
159173
json = (response && response.trim() !== '' ? JSON.parse(response) : {});
160174
if (status >= 200 && status <= 299) {
161-
return typeof success === "function" ? success(json) : void 0;
175+
return resolve({
176+
data: json,
177+
status: status
178+
});
162179
} else if (status === 401) {
163-
return self.fire('grapp-authentication-error');
180+
self.fire('grapp-authentication-error');
181+
return reject({
182+
data: json,
183+
status: status
184+
});
164185
} else {
165-
return typeof error === "function" ? error(json) : void 0;
186+
return reject({
187+
data: json,
188+
status: status
189+
});
166190
}
167191
}
168192
});

index.html

+6-4
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ <h2>Demo</h2>
3232

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

35-
<template is="dom-repeat" items="[[data]]">
35+
<div>Response code: <span>[[status]]</span></div>
36+
<template is="dom-repeat" items="[[items]]">
3637
<p>
3738
<span>{{item.name}}</span>: <span>{{item.role}}</span>
3839
</p>
@@ -44,9 +45,10 @@ <h2>Demo</h2>
4445
Polymer({
4546
is: 'demo-element',
4647
ready: function() {
47-
this.$.data.resource.index((function(result) {
48-
this.data = result;
49-
}).bind(this));
48+
this.$.data.resource.index().then(function(response) {
49+
this.items = response.data;
50+
this.status = response.status;
51+
}.bind(this));
5052
}
5153
});
5254
</script>

src/grapp-rest-resource.coffee

+32-24
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,40 @@ Polymer
2020
self = @
2121

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

27-
show: (id, success, error) ->
28-
self._sendRequest('GET', self.showUrl, id).then (request) ->
29-
self._handleResponse request.response, request.xhr.status, success, error
28+
show: (id) ->
29+
new Promise (resolve, reject) ->
30+
self._sendRequest('GET', self.showUrl, id).then (request) ->
31+
self._handleResponse request.response, request.xhr.status, resolve, reject
3032

31-
new: (success, error) ->
32-
self._sendRequest('GET', self.newUrl, null, 'new').then (request) ->
33-
self._handleResponse request.response, request.xhr.status, success, error
33+
new: () ->
34+
new Promise (resolve, reject) ->
35+
self._sendRequest('GET', self.newUrl, null, 'new').then (request) ->
36+
self._handleResponse request.response, request.xhr.status, resolve, reject
3437

35-
create: (data, success, error) ->
36-
self._sendRequest('POST', self.createUrl, null, data).then (request) ->
37-
self._handleResponse request.response, request.xhr.status, success, error
38+
create: (data) ->
39+
new Promise (resolve, reject) ->
40+
self._sendRequest('POST', self.createUrl, null, data).then (request) ->
41+
self._handleResponse request.response, request.xhr.status, resolve, reject
3842

39-
update: (id, data, success, error) ->
40-
self._sendRequest('PUT', self.updateUrl, id, data).then (request) ->
41-
self._handleResponse request.response, request.xhr.status, success, error
43+
update: (id, data) ->
44+
new Promise (resolve, reject) ->
45+
self._sendRequest('PUT', self.updateUrl, id, data).then (request) ->
46+
self._handleResponse request.response, request.xhr.status, resolve, reject
4247

43-
destroy: (id, success, error) ->
44-
self._sendRequest('DELETE', self.destroyUrl, id).then (request) ->
45-
self._handleResponse request.response, request.xhr.status, success, error
48+
destroy: (id) ->
49+
new Promise (resolve, reject) ->
50+
self._sendRequest('DELETE', self.destroyUrl, id).then (request) ->
51+
self._handleResponse request.response, request.xhr.status, resolve, reject
4652

47-
memberAction: (id, action, success, error) ->
48-
self._sendRequest('PUT', self.memberUrl, id, action).then (request) ->
49-
self._handleResponse request.response, request.xhr.status, success, error
53+
memberAction: (id, action) ->
54+
new Promise (resolve, reject) ->
55+
self._sendRequest('PUT', self.memberUrl, id, action).then (request) ->
56+
self._handleResponse request.response, request.xhr.status, resolve, reject
5057

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

77-
_handleResponse: (response, status, success, error) ->
84+
_handleResponse: (response, status, resolve, reject) ->
7885
json = (if response && response.trim() != '' then JSON.parse response else {})
7986
if status >= 200 && status <= 299
80-
success? json
87+
resolve data: json, status: status
8188
else if status == 401
8289
self.fire 'grapp-authentication-error'
90+
reject data: json, status: status
8391
else
84-
error? json
92+
reject data: json, status: status

test/basic.html

+4-4
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,16 @@
2525

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

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

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

4040
</script>

0 commit comments

Comments
 (0)