OLD | NEW |
(Empty) | |
| 1 |
| 2 |
| 3 Polymer('core-ajax', { |
| 4 /** |
| 5 * Fired when a response is received. |
| 6 * |
| 7 * @event core-response |
| 8 */ |
| 9 |
| 10 /** |
| 11 * Fired when an error is received. |
| 12 * |
| 13 * @event core-error |
| 14 */ |
| 15 |
| 16 /** |
| 17 * Fired whenever a response or an error is received. |
| 18 * |
| 19 * @event core-complete |
| 20 */ |
| 21 |
| 22 /** |
| 23 * The URL target of the request. |
| 24 * |
| 25 * @attribute url |
| 26 * @type string |
| 27 * @default '' |
| 28 */ |
| 29 url: '', |
| 30 |
| 31 /** |
| 32 * Specifies what data to store in the `response` property, and |
| 33 * to deliver as `event.response` in `response` events. |
| 34 * |
| 35 * One of: |
| 36 * |
| 37 * `text`: uses `XHR.responseText`. |
| 38 * |
| 39 * `xml`: uses `XHR.responseXML`. |
| 40 * |
| 41 * `json`: uses `XHR.responseText` parsed as JSON. |
| 42 * |
| 43 * `arraybuffer`: uses `XHR.response`. |
| 44 * |
| 45 * `blob`: uses `XHR.response`. |
| 46 * |
| 47 * `document`: uses `XHR.response`. |
| 48 * |
| 49 * @attribute handleAs |
| 50 * @type string |
| 51 * @default 'text' |
| 52 */ |
| 53 handleAs: '', |
| 54 |
| 55 /** |
| 56 * If true, automatically performs an Ajax request when either `url` or `par
ams` changes. |
| 57 * |
| 58 * @attribute auto |
| 59 * @type boolean |
| 60 * @default false |
| 61 */ |
| 62 auto: false, |
| 63 |
| 64 /** |
| 65 * Parameters to send to the specified URL, as JSON. |
| 66 * |
| 67 * @attribute params |
| 68 * @type string (JSON) |
| 69 * @default '' |
| 70 */ |
| 71 params: '', |
| 72 |
| 73 /** |
| 74 * Returns the response object. |
| 75 * |
| 76 * @attribute response |
| 77 * @type Object |
| 78 * @default null |
| 79 */ |
| 80 response: null, |
| 81 |
| 82 /** |
| 83 * The HTTP method to use such as 'GET', 'POST', 'PUT', or 'DELETE'. |
| 84 * Default is 'GET'. |
| 85 * |
| 86 * @attribute method |
| 87 * @type string |
| 88 * @default '' |
| 89 */ |
| 90 method: '', |
| 91 |
| 92 /** |
| 93 * HTTP request headers to send. |
| 94 * |
| 95 * Example: |
| 96 * |
| 97 * <core-ajax |
| 98 * auto |
| 99 * url="http://somesite.com" |
| 100 * headers='{"X-Requested-With": "XMLHttpRequest"}' |
| 101 * handleAs="json" |
| 102 * on-core-response="{{handleResponse}}"></core-ajax> |
| 103 * |
| 104 * @attribute headers |
| 105 * @type Object |
| 106 * @default null |
| 107 */ |
| 108 headers: null, |
| 109 |
| 110 /** |
| 111 * Optional raw body content to send when method === "POST". |
| 112 * |
| 113 * Example: |
| 114 * |
| 115 * <core-ajax method="POST" auto url="http://somesite.com" |
| 116 * body='{"foo":1, "bar":2}'> |
| 117 * </core-ajax> |
| 118 * |
| 119 * @attribute body |
| 120 * @type Object |
| 121 * @default null |
| 122 */ |
| 123 body: null, |
| 124 |
| 125 /** |
| 126 * Content type to use when sending data. |
| 127 * |
| 128 * @attribute contentType |
| 129 * @type string |
| 130 * @default 'application/x-www-form-urlencoded' |
| 131 */ |
| 132 contentType: 'application/x-www-form-urlencoded', |
| 133 |
| 134 /** |
| 135 * Set the withCredentials flag on the request. |
| 136 * |
| 137 * @attribute withCredentials |
| 138 * @type boolean |
| 139 * @default false |
| 140 */ |
| 141 withCredentials: false, |
| 142 |
| 143 /** |
| 144 * Additional properties to send to core-xhr. |
| 145 * |
| 146 * Can be set to an object containing default properties |
| 147 * to send as arguments to the `core-xhr.request()` method |
| 148 * which implements the low-level communication. |
| 149 * |
| 150 * @property xhrArgs |
| 151 * @type Object |
| 152 * @default null |
| 153 */ |
| 154 xhrArgs: null, |
| 155 |
| 156 ready: function() { |
| 157 this.xhr = document.createElement('core-xhr'); |
| 158 }, |
| 159 |
| 160 receive: function(response, xhr) { |
| 161 if (this.isSuccess(xhr)) { |
| 162 this.processResponse(xhr); |
| 163 } else { |
| 164 this.error(xhr); |
| 165 } |
| 166 this.complete(xhr); |
| 167 }, |
| 168 |
| 169 isSuccess: function(xhr) { |
| 170 var status = xhr.status || 0; |
| 171 return !status || (status >= 200 && status < 300); |
| 172 }, |
| 173 |
| 174 processResponse: function(xhr) { |
| 175 var response = this.evalResponse(xhr); |
| 176 this.response = response; |
| 177 this.fire('core-response', {response: response, xhr: xhr}); |
| 178 }, |
| 179 |
| 180 error: function(xhr) { |
| 181 var response = xhr.status + ': ' + xhr.responseText; |
| 182 this.fire('core-error', {response: response, xhr: xhr}); |
| 183 }, |
| 184 |
| 185 complete: function(xhr) { |
| 186 this.fire('core-complete', {response: xhr.status, xhr: xhr}); |
| 187 }, |
| 188 |
| 189 evalResponse: function(xhr) { |
| 190 return this[(this.handleAs || 'text') + 'Handler'](xhr); |
| 191 }, |
| 192 |
| 193 xmlHandler: function(xhr) { |
| 194 return xhr.responseXML; |
| 195 }, |
| 196 |
| 197 textHandler: function(xhr) { |
| 198 return xhr.responseText; |
| 199 }, |
| 200 |
| 201 jsonHandler: function(xhr) { |
| 202 var r = xhr.responseText; |
| 203 try { |
| 204 return JSON.parse(r); |
| 205 } catch (x) { |
| 206 console.warn('core-ajax caught an exception trying to parse reponse as J
SON:'); |
| 207 console.warn('url:', this.url); |
| 208 console.warn(x); |
| 209 return r; |
| 210 } |
| 211 }, |
| 212 |
| 213 documentHandler: function(xhr) { |
| 214 return xhr.response; |
| 215 }, |
| 216 |
| 217 blobHandler: function(xhr) { |
| 218 return xhr.response; |
| 219 }, |
| 220 |
| 221 arraybufferHandler: function(xhr) { |
| 222 return xhr.response; |
| 223 }, |
| 224 |
| 225 urlChanged: function() { |
| 226 if (!this.handleAs) { |
| 227 var ext = String(this.url).split('.').pop(); |
| 228 switch (ext) { |
| 229 case 'json': |
| 230 this.handleAs = 'json'; |
| 231 break; |
| 232 } |
| 233 } |
| 234 this.autoGo(); |
| 235 }, |
| 236 |
| 237 paramsChanged: function() { |
| 238 this.autoGo(); |
| 239 }, |
| 240 |
| 241 autoChanged: function() { |
| 242 this.autoGo(); |
| 243 }, |
| 244 |
| 245 // TODO(sorvell): multiple side-effects could call autoGo |
| 246 // during one micro-task, use a job to have only one action |
| 247 // occur |
| 248 autoGo: function() { |
| 249 if (this.auto) { |
| 250 this.goJob = this.job(this.goJob, this.go, 0); |
| 251 } |
| 252 }, |
| 253 |
| 254 /** |
| 255 * Performs an Ajax request to the specified URL. |
| 256 * |
| 257 * @method go |
| 258 */ |
| 259 go: function() { |
| 260 var args = this.xhrArgs || {}; |
| 261 // TODO(sjmiles): we may want XHR to default to POST if body is set |
| 262 args.body = this.body || args.body; |
| 263 args.params = this.params || args.params; |
| 264 if (args.params && typeof(args.params) == 'string') { |
| 265 args.params = JSON.parse(args.params); |
| 266 } |
| 267 args.headers = this.headers || args.headers || {}; |
| 268 if (args.headers && typeof(args.headers) == 'string') { |
| 269 args.headers = JSON.parse(args.headers); |
| 270 } |
| 271 if (this.contentType) { |
| 272 args.headers['content-type'] = this.contentType; |
| 273 } |
| 274 if (this.handleAs === 'arraybuffer' || this.handleAs === 'blob' || |
| 275 this.handleAs === 'document') { |
| 276 args.responseType = this.handleAs; |
| 277 } |
| 278 args.withCredentials = this.withCredentials; |
| 279 args.callback = this.receive.bind(this); |
| 280 args.url = this.url; |
| 281 args.method = this.method; |
| 282 return args.url && this.xhr.request(args); |
| 283 } |
| 284 |
| 285 }); |
| 286 |
OLD | NEW |