Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(884)

Side by Side Diff: pkg/polymer/lib/elements/polymer-ajax/polymer-ajax.html

Issue 175443005: [polymer] import all elements (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: updated from bower Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 /**
12 * The `polymer-ajax` element performs `XMLHttpRequest`s.
13 *
14 * You can trigger a request explicitly by calling `go` on the
15 * element.
16 *
17 * With `auto` set to `true`, the element performs a request whenever
18 * its `url` or `params` properties are changed.
19 *
20 *
21 * Example:
22 *
23 * <polymer-ajax auto url="http://gdata.youtube.com/feeds/api/videos/"
24 * params='{"alt":"json", "q":"chrome"}'
25 * handleAs="json"
26 * on-polymer-response="{{handleResponse}}">
27 * </polymer-ajax>
28 *
29 * Note: The `params` attribute must be double quoted JSON.
30 *
31 * @class polymer-ajax
32 * @status beta
33 * @homepage github.io
34 *
35 */
36
37 /**
38 * Fired when a response is received.
39 *
40 * @event polymer-response
41 */
42
43 /**
44 * Fired when an error is received.
45 *
46 * @event polymer-error
47 */
48
49 /**
50 * Fired whenever a response or an error is received.
51 *
52 * @event polymer-complete
53 */
54
55 -->
56 <link rel="import" href="../polymer/polymer.html">
57 <link rel="import" href="polymer-xhr.html">
58
59 <polymer-element name="polymer-ajax" attributes="url handleAs auto params respon se method headers body contentType">
60 <script>
61 Polymer('polymer-ajax', {
62 /**
63 * The URL target of the request.
64 *
65 * @attribute url
66 * @type string
67 * @default ''
68 */
69 url: '',
70 /**
71 * Specifies what data to store in the `response` property, and
72 * to deliver as `event.response` in `response` events.
73 *
74 * One of:
75 *
76 * `text`: uses `XHR.responseText`.
77 *
78 * `xml`: uses `XHR.responseXML`.
79 *
80 * `json`: uses `XHR.responseText` parsed as JSON.
81 *
82 * @attribute handleAs
83 * @type string
84 * @default 'text'
85 */
86 handleAs: '',
87 /**
88 * If true, automatically performs an Ajax request when either `url` or `p arams` changes.
89 *
90 * @attribute auto
91 * @type boolean
92 * @default false
93 */
94 auto: false,
95 /**
96 * Parameters to send to the specified URL, as JSON.
97 *
98 * @attribute params
99 * @type string (JSON)
100 * @default ''
101 */
102 params: '',
103 /**
104 * Returns the response object.
105 *
106 * @attribute response
107 * @type Object
108 * @default null
109 */
110 response: null,
111 /**
112 * The HTTP method to use such as 'GET', 'POST', 'PUT', or 'DELETE'.
113 * Default is 'GET'.
114 *
115 * @attribute method
116 * @type string
117 * @default ''
118 */
119 method: '',
120 /**
121 * HTTP request headers to send.
122 *
123 * Example:
124 *
125 * <polymer-ajax auto url="http://somesite.com"
126 * headers='{"X-Requested-With": "XMLHttpRequest"}'
127 * handleAs="json"
128 * on-polymer-response="{{handleResponse}}">
129 * </polymer-ajax>
130 *
131 * @attribute headers
132 * @type Object
133 * @default null
134 */
135 headers: null,
136 /**
137 * Optional raw body content to send when method === "POST".
138 *
139 * Example:
140 *
141 * <polymer-ajax method="POST" auto url="http://somesite.com"
142 * body='{"foo":1, "bar":2}'>
143 * </polymer-ajax>
144 *
145 * @attribute body
146 * @type Object
147 * @default null
148 */
149 body: null,
150 /**
151 * Content type to use when sending data.
152 *
153 * @attribute contentType
154 * @type string
155 * @default 'application/x-www-form-urlencoded'
156 */
157 contentType: 'application/x-www-form-urlencoded',
158 ready: function() {
159 this.xhr = document.createElement('polymer-xhr');
160 },
161 receive: function(response, xhr) {
162 if (this.isSuccess(xhr)) {
163 this.processResponse(xhr);
164 } else {
165 this.error(xhr);
166 }
167 this.complete(xhr);
168 },
169 isSuccess: function(xhr) {
170 var status = xhr.status || 0;
171 return !status || (status >= 200 && status < 300);
172 },
173 processResponse: function(xhr) {
174 var response = this.evalResponse(xhr);
175 this.response = response;
176 this.fire('polymer-response', {response: response, xhr: xhr});
177 },
178 error: function(xhr) {
179 var response = xhr.status + ': ' + xhr.responseText;
180 this.fire('polymer-error', {response: response, xhr: xhr});
181 },
182 complete: function(xhr) {
183 this.fire('polymer-complete', {response: xhr.status, xhr: xhr});
184 },
185 evalResponse: function(xhr) {
186 return this[(this.handleAs || 'text') + 'Handler'](xhr);
187 },
188 xmlHandler: function(xhr) {
189 return xhr.responseXML;
190 },
191 textHandler: function(xhr) {
192 return xhr.responseText;
193 },
194 jsonHandler: function(xhr) {
195 var r = xhr.responseText;
196 try {
197 return JSON.parse(r);
198 } catch (x) {
199 return r;
200 }
201 },
202 urlChanged: function() {
203 if (!this.handleAs) {
204 var ext = String(this.url).split('.').pop();
205 switch (ext) {
206 case 'json':
207 this.handleAs = 'json';
208 break;
209 }
210 }
211 this.autoGo();
212 },
213 paramsChanged: function() {
214 this.autoGo();
215 },
216 autoChanged: function() {
217 this.autoGo();
218 },
219 // TODO(sorvell): multiple side-effects could call autoGo
220 // during one micro-task, use a job to have only one action
221 // occur
222 autoGo: function() {
223 if (this.auto) {
224 this.goJob = this.job(this.goJob, this.go, 0);
225 }
226 },
227 /**
228 * Performs an Ajax request to the specified URL.
229 *
230 * @method go
231 */
232 go: function() {
233 var args = this.xhrArgs || {};
234 // TODO(sjmiles): alternatively, we could force POST if body is set
235 if (this.method === 'POST') {
236 args.body = this.body || args.body;
237 }
238 args.params = this.params || args.params;
239 if (args.params && typeof(args.params) == 'string') {
240 args.params = JSON.parse(args.params);
241 }
242 args.headers = this.headers || args.headers || {};
243 if (args.headers && typeof(args.headers) == 'string') {
244 args.headers = JSON.parse(args.headers);
245 }
246 if (this.contentType) {
247 args.headers['content-type'] = this.contentType;
248 }
249 args.callback = this.receive.bind(this);
250 args.url = this.url;
251 args.method = this.method;
252 return args.url && this.xhr.request(args);
253 }
254 });
255 </script>
256 </polymer-element>
OLDNEW
« no previous file with comments | « pkg/polymer/lib/elements/polymer-ajax/index.html ('k') | pkg/polymer/lib/elements/polymer-ajax/polymer-xhr.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698