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