Skip to content

Commit

Permalink
feat: Add new method (GET /resource/new)
Browse files Browse the repository at this point in the history
doc: Add resource object method documentation
  • Loading branch information
grappendorf committed Dec 18, 2014
1 parent 8448962 commit 0bb1c9f
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 10 deletions.
55 changes: 55 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ Attributes

`url="/users/:userId?q={{searchText}}"`

Default URLS:

Method | HTTP method | URL
------------- | ----------- | ---
index | GET | /users
show | GET | /users/1
new | GET | /users/new
create | POST | /users
update | PUT | /users
delete | DELETE | /users/1
member foo | PUT | /users/1/foo

* **params**

- *type:* hash
Expand All @@ -40,6 +52,12 @@ Attributes

Use this URL instead of the default one for "GET /resources/:id" requests.

* **newUrl**

- *type:* string

Use this URL instead of the default one for "GET /resources/:id" requests.

* **createUrl**

- *type:* string
Expand Down Expand Up @@ -71,3 +89,40 @@ Events
* **grapp-authentication-error**

Is fired when a REST API call returns a 401 error.


Resource Object methods
-----------------------

* **index(successCallback, errorCallback)**

Performs a HTTP GET on the index URL and returns the response data.

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

Performs a HTTP GET on the show URL with the specified resource id and returns the response
data.

* **new(successCallback, errorCallback)**

Performs a HTTP GET on the new URL and returns the response data.

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

Performs a HTTP POST on the create URL with the specified resource data and returns
the response data.

* **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.

* **delete(id, successCallback, errorCallback)**

Performs a HTTP DELETE on the delete URL with the specified resource id and returns the response
data.

* **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.
30 changes: 25 additions & 5 deletions grapp-rest-resource.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<link href="../core-ajax/core-xhr.html" rel="import">

<polymer-element name="grapp-rest-resource"
attributes="url params resource indexUrl showUrl updateUrl createUrl deleteUrl memberUrl">
attributes="url params resource indexUrl showUrl newUrl createUrl updateUrl deleteUrl memberUrl">

<template>

Expand Down Expand Up @@ -96,14 +96,12 @@
}
});
},
update: function(id, data, success, error) {
"new": function(success, error) {
return self.$.xhr.request({
method: 'PUT',
url: prepareUrl(self.updateUrl || self.url, self.params, id),
url: prepareUrl(self.newUrl || self.url, self.params, null, 'new'),
headers: {
'Accept': 'application/json'
},
body: JSON.stringify(data),
callback: function(data, response) {
var json;
json = (data.trim() !== '' ? JSON.parse(data) : {});
Expand Down Expand Up @@ -140,6 +138,28 @@
}
});
},
update: function(id, data, success, error) {
return self.$.xhr.request({
method: 'PUT',
url: prepareUrl(self.updateUrl || self.url, self.params, id),
headers: {
'Accept': 'application/json'
},
body: JSON.stringify(data),
callback: function(data, response) {
var json;
json = (data.trim() !== '' ? JSON.parse(data) : {});
switch (response.status) {
case 200:
return typeof success === "function" ? success(json) : void 0;
case 401:
return self.fire('grapp-authentication-error');
default:
return typeof error === "function" ? error(json) : void 0;
}
}
});
},
"delete": function(id, success, error) {
return self.$.xhr.request({
method: 'DELETE',
Expand Down
20 changes: 16 additions & 4 deletions src/grapp-rest-resource.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,10 @@ Polymer 'grapp-rest-resource',
else
error? json

update: (id, data, success, error) ->
new: (success, error) ->
self.$.xhr.request
method: 'PUT'
url: prepareUrl self.updateUrl || self.url, self.params, id
url: prepareUrl self.newUrl || self.url, self.params, null, 'new'
headers: {'Accept': 'application/json'}
body: JSON.stringify data
callback: (data, response) ->
json = (if data.trim() != '' then JSON.parse data else {})
switch response.status
Expand All @@ -69,6 +67,20 @@ Polymer 'grapp-rest-resource',
else
error? json

update: (id, data, success, error) ->
self.$.xhr.request
method: 'PUT'
url: prepareUrl self.updateUrl || self.url, self.params, id
headers: {'Accept': 'application/json'}
body: JSON.stringify data
callback: (data, response) ->
json = (if data.trim() != '' then JSON.parse data else {})
switch response.status
when 200 then success? json
when 401 then self.fire 'grapp-authentication-error'
else
error? json

delete: (id, success, error) ->
self.$.xhr.request
method: 'DELETE'
Expand Down
2 changes: 1 addition & 1 deletion src/grapp-rest-resource.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<link href="../core-ajax/core-xhr.html" rel="import">

<polymer-element name="grapp-rest-resource"
attributes="url params resource indexUrl showUrl updateUrl createUrl deleteUrl memberUrl">
attributes="url params resource indexUrl showUrl newUrl createUrl updateUrl deleteUrl memberUrl">

<template>

Expand Down

0 comments on commit 0bb1c9f

Please sign in to comment.