| Index: pkg/polymer/lib/elements/polymer-ajax/polymer-xhr.html
|
| diff --git a/pkg/polymer/lib/elements/polymer-ajax/polymer-xhr.html b/pkg/polymer/lib/elements/polymer-ajax/polymer-xhr.html
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..b5859d21767347e92ee358e6d6fe468d9ee2e5b7
|
| --- /dev/null
|
| +++ b/pkg/polymer/lib/elements/polymer-ajax/polymer-xhr.html
|
| @@ -0,0 +1,96 @@
|
| +<!--
|
| +Copyright 2013 The Polymer Authors. All rights reserved.
|
| +Use of this source code is governed by a BSD-style
|
| +license that can be found in the LICENSE file.
|
| +-->
|
| +<!--
|
| +/**
|
| + * @module Polymer Elements
|
| + */
|
| +/**
|
| + * polymer-xhr can be used to perform XMLHttpRequests.
|
| +
|
| + * Example:
|
| + *
|
| + * <polymer-xhr id="xhr"></polymer-xhr>
|
| + * ...
|
| + * this.$.xhr.request({url: url, params: params, callback: callback});
|
| + *
|
| + * @class polymer-xhr
|
| + */
|
| +-->
|
| +<link rel="import" href="../polymer/polymer.html">
|
| +
|
| +<polymer-element name="polymer-xhr">
|
| + <template>
|
| + <style>
|
| + :host {
|
| + display: none;
|
| + }
|
| + </style>
|
| + </template>
|
| + <script>
|
| + Polymer('polymer-xhr', {
|
| + makeReadyStateHandler: function(xhr, callback) {
|
| + xhr.onreadystatechange = function() {
|
| + if (xhr.readyState == 4) {
|
| + callback && callback.call(null, xhr.response, xhr);
|
| + }
|
| + };
|
| + },
|
| + setRequestHeaders: function(xhr, headers) {
|
| + if (headers) {
|
| + for (var name in headers) {
|
| + xhr.setRequestHeader(name, headers[name]);
|
| + }
|
| + }
|
| + },
|
| + toQueryString: function(params) {
|
| + var r = [];
|
| + for (var n in params) {
|
| + var v = params[n];
|
| + n = encodeURIComponent(n);
|
| + r.push(v == null ? n : (n + '=' + encodeURIComponent(v)));
|
| + }
|
| + return r.join('&');
|
| + },
|
| + /**
|
| + * Sends a HTTP request to the server and returns the XHR object.
|
| + *
|
| + * @method request
|
| + * @param {Object} inOptions
|
| + * @param {String} inOptions.url The url to which the request is sent.
|
| + * @param {String} inOptions.method The HTTP method to use, default is GET.
|
| + * @param {boolean} inOptions.sync By default, all requests are sent asynchronously.
|
| + * To send synchronous requests, set to true.
|
| + * @param {Object} inOptions.params Data to be sent to the server.
|
| + * @param {Object} inOptions.body The content for the request body for POST method.
|
| + * @param {Object} inOptions.headers HTTP request headers.
|
| + * @param {String} inOptions.responseType The response type. Default is 'text'.
|
| + * @param {Object} inOptions.callback Called when request is completed.
|
| + * @returns {Object} XHR object.
|
| + */
|
| + request: function(options) {
|
| + var xhr = new XMLHttpRequest();
|
| + var url = options.url;
|
| + var method = options.method || 'GET';
|
| + var async = !options.sync;
|
| + var params = this.toQueryString(options.params);
|
| + if (params && method == 'GET') {
|
| + url += (url.indexOf('?') > 0 ? '&' : '?') + params;
|
| + }
|
| + xhr.open(method, url, async);
|
| + if (options.responseType) {
|
| + xhr.responseType = options.responseType;
|
| + }
|
| + this.makeReadyStateHandler(xhr, options.callback);
|
| + this.setRequestHeaders(xhr, options.headers);
|
| + xhr.send(method == 'POST' ? (options.body || params) : null);
|
| + if (!async) {
|
| + xhr.onreadystatechange(xhr);
|
| + }
|
| + return xhr;
|
| + }
|
| + });
|
| + </script>
|
| +</polymer-element>
|
|
|