-
Notifications
You must be signed in to change notification settings - Fork 0
/
lancie-ajax.html
160 lines (146 loc) · 3.86 KB
/
lancie-ajax.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<!--
@license
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../iron-ajax/iron-ajax.html">
<dom-module id="lancie-ajax">
<template>
<iron-ajax
id="ajax"
headers="[[createHeader(token, contenttype)]]"
body="[[body]]"
params="[[params]]"
method="[[method]]"
url="[[_getURL(absoluteurl, refurl)]]"
handle-as="[[handleAs]]"
last-response="{{lastResponse}}"
loading="{{loading}}"
debounce-duration="300"
on-response="handleResponse"
on-error="handleResponse"
></iron-ajax>
</template>
<script>
(function() {
'use strict';
var token, ajax = [];
Polymer({
is: 'lancie-ajax',
properties: {
contenttype: {
type: String,
value: 'application/json',
notify: true
},
refurl: {
type: String,
value: '',
notify: true
},
absoluteurl: {
type: String,
value: '',
notify: true
},
handleAs: {
type: String,
value: 'json',
notify: true
},
autoFire: {
type: Boolean,
value: false
},
skipToken: {
type: Boolean,
value: false
},
isuser: {
type: Boolean,
value: false
},
loading: {
type: Boolean,
notify: true,
value: false
},
params: {
type: Object,
value: {}
},
method: {
type: String,
value: 'GET'
},
lastResponse: {
type: Object,
notify: true
},
body: {
type: Object
},
token: {
type: String,
value: '',
notify: true,
}
},
/* STATIC CALL */
injectToken: function(newToken) {
token = newToken;
for (let i = 0; i < ajax.length; i++) {
ajax[i](token);
}
},
attached: function() {
if (this.skipToken && this.autoFire) {
this.generateRequest();
return;
}
if (token) {
this.token = token;
if (this.autoFire) {
this.generateRequest();
}
} else {
ajax.push(function(newToken) {
this.token = newToken;
if (this.autoFire) {
this.generateRequest();
}
}.bind(this));
}
},
_getURL: function(absoluteurl, refurl) {
if (absoluteurl !== '') {
return absoluteurl;
}
return '/api/v1/' + refurl;
},
createHeader: function(token, contenttype) {
if (this.absoluteurl !== '') {
return {
'Content-Type': contenttype,
};
}
return {
'Content-Type': contenttype,
'X-Auth-Token': token,
};
},
handleResponse: function(event) {
this.fire('lancie-ajax', event.detail);
},
generateRequest: function() {
this.$.ajax.generateRequest();
},
});
})();
</script>
</dom-module>