OLD | NEW |
(Empty) | |
| 1 <!-- |
| 2 Copyright 2013 The Polymer Authors. All rights reserved. |
| 3 Use of this source code is governed by a BSD-style |
| 4 license that can be found in the LICENSE file. |
| 5 --> |
| 6 <!-- |
| 7 /** |
| 8 * @module Polymer Elements |
| 9 */ |
| 10 /** |
| 11 * polymer-xhr can be used to perform XMLHttpRequests. |
| 12 |
| 13 * Example: |
| 14 * |
| 15 * <polymer-xhr id="xhr"></polymer-xhr> |
| 16 * ... |
| 17 * this.$.xhr.request({url: url, params: params, callback: callback}); |
| 18 * |
| 19 * @class polymer-xhr |
| 20 */ |
| 21 --> |
| 22 <link rel="import" href="../polymer/polymer.html"> |
| 23 |
| 24 <polymer-element name="polymer-xhr"> |
| 25 <template> |
| 26 <style> |
| 27 :host { |
| 28 display: none; |
| 29 } |
| 30 </style> |
| 31 </template> |
| 32 <script> |
| 33 Polymer('polymer-xhr', { |
| 34 makeReadyStateHandler: function(xhr, callback) { |
| 35 xhr.onreadystatechange = function() { |
| 36 if (xhr.readyState == 4) { |
| 37 callback && callback.call(null, xhr.response, xhr); |
| 38 } |
| 39 }; |
| 40 }, |
| 41 setRequestHeaders: function(xhr, headers) { |
| 42 if (headers) { |
| 43 for (var name in headers) { |
| 44 xhr.setRequestHeader(name, headers[name]); |
| 45 } |
| 46 } |
| 47 }, |
| 48 toQueryString: function(params) { |
| 49 var r = []; |
| 50 for (var n in params) { |
| 51 var v = params[n]; |
| 52 n = encodeURIComponent(n); |
| 53 r.push(v == null ? n : (n + '=' + encodeURIComponent(v))); |
| 54 } |
| 55 return r.join('&'); |
| 56 }, |
| 57 /** |
| 58 * Sends a HTTP request to the server and returns the XHR object. |
| 59 * |
| 60 * @method request |
| 61 * @param {Object} inOptions |
| 62 * @param {String} inOptions.url The url to which the request is sent. |
| 63 * @param {String} inOptions.method The HTTP method to use, default is
GET. |
| 64 * @param {boolean} inOptions.sync By default, all requests are sent as
ynchronously. |
| 65 * To send synchronous requests, set to true. |
| 66 * @param {Object} inOptions.params Data to be sent to the server. |
| 67 * @param {Object} inOptions.body The content for the request body for
POST method. |
| 68 * @param {Object} inOptions.headers HTTP request headers. |
| 69 * @param {String} inOptions.responseType The response type. Default is
'text'. |
| 70 * @param {Object} inOptions.callback Called when request is completed. |
| 71 * @returns {Object} XHR object. |
| 72 */ |
| 73 request: function(options) { |
| 74 var xhr = new XMLHttpRequest(); |
| 75 var url = options.url; |
| 76 var method = options.method || 'GET'; |
| 77 var async = !options.sync; |
| 78 var params = this.toQueryString(options.params); |
| 79 if (params && method == 'GET') { |
| 80 url += (url.indexOf('?') > 0 ? '&' : '?') + params; |
| 81 } |
| 82 xhr.open(method, url, async); |
| 83 if (options.responseType) { |
| 84 xhr.responseType = options.responseType; |
| 85 } |
| 86 this.makeReadyStateHandler(xhr, options.callback); |
| 87 this.setRequestHeaders(xhr, options.headers); |
| 88 xhr.send(method == 'POST' ? (options.body || params) : null); |
| 89 if (!async) { |
| 90 xhr.onreadystatechange(xhr); |
| 91 } |
| 92 return xhr; |
| 93 } |
| 94 }); |
| 95 </script> |
| 96 </polymer-element> |
OLD | NEW |