Skip to content

Commit

Permalink
feat: Allow additional headers to be sent with each request
Browse files Browse the repository at this point in the history
  • Loading branch information
Dirk Grappendorf committed Mar 9, 2015
1 parent 33b2975 commit 6742318
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 31 deletions.
46 changes: 23 additions & 23 deletions grapp-rest-resource.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

<!--
Copyright (c) 2014 Dirk Grappendorf, www.grappendorf.net. All rights reserved.
Copyright (c) 2014-2015 Dirk Grappendorf, www.grappendorf.net. All rights reserved.
This code may only be used under the MIT style license found in the file LICENSE.txt.
Element: grapp-rest-resource
Expand All @@ -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 newUrl createUrl updateUrl deleteUrl memberUrl">
attributes="url params resource indexUrl showUrl newUrl createUrl updateUrl deleteUrl memberUrl headers">

<template>

Expand Down Expand Up @@ -54,14 +54,13 @@
Polymer('grapp-rest-resource', {
created: function() {
var self;
this.headers = {};
self = this;
return this.resource = {
index: function(success, error) {
return self.$.xhr.request({
url: prepareUrl(self.indexUrl || self.url, self.params),
headers: {
'Accept': 'application/json'
},
headers: self.prepareHeaders(),
callback: function(data, response) {
var json;
json = (data.trim() !== '' ? JSON.parse(data) : {});
Expand All @@ -79,9 +78,7 @@
show: function(id, success, error) {
return self.$.xhr.request({
url: prepareUrl(self.showUrl || self.url, self.params, id),
headers: {
'Accept': 'application/json'
},
headers: self.prepareHeaders(),
callback: function(data, response) {
var json;
json = (data.trim() !== '' ? JSON.parse(data) : {});
Expand All @@ -99,9 +96,7 @@
"new": function(success, error) {
return self.$.xhr.request({
url: prepareUrl(self.newUrl || self.url, self.params, null, 'new'),
headers: {
'Accept': 'application/json'
},
headers: self.prepareHeaders(),
callback: function(data, response) {
var json;
json = (data.trim() !== '' ? JSON.parse(data) : {});
Expand All @@ -120,9 +115,7 @@
return self.$.xhr.request({
method: 'POST',
url: prepareUrl(self.createUrl || self.url, self.params),
headers: {
'Accept': 'application/json'
},
headers: self.prepareHeaders(),
body: JSON.stringify(data),
callback: function(data, response) {
var json;
Expand All @@ -142,9 +135,7 @@
return self.$.xhr.request({
method: 'PUT',
url: prepareUrl(self.updateUrl || self.url, self.params, id),
headers: {
'Accept': 'application/json'
},
headers: self.prepareHeaders(),
body: JSON.stringify(data),
callback: function(data, response) {
var json;
Expand All @@ -164,9 +155,7 @@
return self.$.xhr.request({
method: 'DELETE',
url: prepareUrl(self.deleteUrl || self.url, self.params, id),
headers: {
'Accept': 'application/json'
},
headers: self.prepareHeaders(),
callback: function(data, response) {
var json;
json = (data.trim() !== '' ? JSON.parse(data) : {});
Expand All @@ -185,9 +174,7 @@
return self.$.xhr.request({
method: 'PUT',
url: prepareUrl(self.memberUrl || self.url, self.params, id, action),
headers: {
'Accept': 'application/json'
},
headers: self.prepareHeaders(),
callback: function(data, response) {
switch (response.status) {
case 200:
Expand All @@ -201,6 +188,19 @@
});
}
};
},
prepareHeaders: function() {
var h, key, val, _ref;
h = {
'Accept': 'application/json'
};
_ref = this.headers;
for (key in _ref) {
val = _ref[key];
h[key] = val;
}
console.log(h);
return h;
}
});

Expand Down
22 changes: 15 additions & 7 deletions src/grapp-rest-resource.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ prepareUrl = (url, params = {}, id = null, action = null) ->
Polymer 'grapp-rest-resource',

created: ->
@headers = {}
self = @

@resource =
index: (success, error) ->
self.$.xhr.request
url: prepareUrl self.indexUrl || self.url, self.params
headers: {'Accept': 'application/json'}
headers: self.prepareHeaders()
callback: (data, response) ->
json = (if data.trim() != '' then JSON.parse data else {})
switch response.status
Expand All @@ -32,7 +33,7 @@ Polymer 'grapp-rest-resource',
show: (id, success, error) ->
self.$.xhr.request
url: prepareUrl self.showUrl || self.url, self.params, id
headers: {'Accept': 'application/json'}
headers: self.prepareHeaders()
callback: (data, response) ->
json = (if data.trim() != '' then JSON.parse data else {})
switch response.status
Expand All @@ -44,7 +45,7 @@ Polymer 'grapp-rest-resource',
new: (success, error) ->
self.$.xhr.request
url: prepareUrl self.newUrl || self.url, self.params, null, 'new'
headers: {'Accept': 'application/json'}
headers: self.prepareHeaders()
callback: (data, response) ->
json = (if data.trim() != '' then JSON.parse data else {})
switch response.status
Expand All @@ -57,7 +58,7 @@ Polymer 'grapp-rest-resource',
self.$.xhr.request
method: 'POST'
url: prepareUrl self.createUrl || self.url, self.params
headers: {'Accept': 'application/json'}
headers: self.prepareHeaders()
body: JSON.stringify data
callback: (data, response) ->
json = (if data.trim() != '' then JSON.parse data else {})
Expand All @@ -71,7 +72,7 @@ Polymer 'grapp-rest-resource',
self.$.xhr.request
method: 'PUT'
url: prepareUrl self.updateUrl || self.url, self.params, id
headers: {'Accept': 'application/json'}
headers: self.prepareHeaders()
body: JSON.stringify data
callback: (data, response) ->
json = (if data.trim() != '' then JSON.parse data else {})
Expand All @@ -85,7 +86,7 @@ Polymer 'grapp-rest-resource',
self.$.xhr.request
method: 'DELETE'
url: prepareUrl self.deleteUrl || self.url, self.params, id
headers: {'Accept': 'application/json'}
headers: self.prepareHeaders()
callback: (data, response) ->
json = (if data.trim() != '' then JSON.parse data else {})
switch response.status
Expand All @@ -98,10 +99,17 @@ Polymer 'grapp-rest-resource',
self.$.xhr.request
method: 'PUT'
url: prepareUrl self.memberUrl || self.url, self.params, id, action
headers: {'Accept': 'application/json'}
headers: self.prepareHeaders()
callback: (data, response) ->
switch response.status
when 200 then success?()
when 401 then self.fire 'grapp-authentication-error'
else
error?()

prepareHeaders: ->
h = {'Accept': 'application/json'}
for key, val of @headers
h[key] = val
console.log h
h
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 newUrl createUrl updateUrl deleteUrl memberUrl">
attributes="url params resource indexUrl showUrl newUrl createUrl updateUrl deleteUrl memberUrl headers">

<template>

Expand Down

0 comments on commit 6742318

Please sign in to comment.