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

Unified Diff: tools/dom/templates/html/impl/impl_XMLHttpRequest.darttemplate

Issue 12087077: Adding ease-of-use methods to HttpRequest. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 11 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « tests/html/xhr_test.dart ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tools/dom/templates/html/impl/impl_XMLHttpRequest.darttemplate
diff --git a/tools/dom/templates/html/impl/impl_XMLHttpRequest.darttemplate b/tools/dom/templates/html/impl/impl_XMLHttpRequest.darttemplate
index ccd909b70d91ea50f0f81897d685cff7ea3f5c64..a983e4f6964ec8ad5e85bec92b153873c27ed5ba 100644
--- a/tools/dom/templates/html/impl/impl_XMLHttpRequest.darttemplate
+++ b/tools/dom/templates/html/impl/impl_XMLHttpRequest.darttemplate
@@ -30,25 +30,39 @@ part of $LIBRARYNAME;
* * [Using XMLHttpRequest](https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest/Using_XMLHttpRequest)
*/
$(ANNOTATIONS)class $CLASSNAME$EXTENDS$IMPLEMENTS$NATIVESPEC {
+
/**
- * Creates a URL get request for the specified `url`.
+ * Creates a URL get request for the specified [url].
+ *
+ * The server response must be a `text/` mime type for this request to
+ * succeed.
+ *
+ * This is similar to [request] but specialized for HTTP GET requests which
+ * return text content.
+ *
+ * See also:
*
- * After completing the request, the object will call the user-provided
- * [onComplete] callback.
+ * * [request]
*/
- factory $CLASSNAME.get(String url, onComplete($CLASSNAME request)) =>
- _HttpRequestUtils.get(url, onComplete, false);
+ static Future<String> getString(String url,
+ {bool withCredentials, void onProgress(ProgressEvent e)}) {
+ return request(url, withCredentials: withCredentials,
+ onProgress: onProgress).then((xhr) => xhr.responseText);
+ }
- // 80 char issue for comments in lists: dartbug.com/7588.
/**
- * Creates a URL GET request for the specified `url` with
- * credentials such a cookie (already) set in the header or
- * [authorization headers](http://tools.ietf.org/html/rfc1945#section-10.2).
+ * Creates a URL request for the specified [url].
*
- * After completing the request, the object will call the user-provided
- * [onComplete] callback.
+ * By default this will do an HTTP GET request, this can be overridden with
+ * [method].
*
- * A few other details to keep in mind when using credentials:
+ * The Future is completed when the response is available.
+ *
+ * The [withCredentials] parameter specified that credentials such as a cookie
+ * (already) set in the header or
+ * [authorization headers](http://tools.ietf.org/html/rfc1945#section-10.2)
+ * should be specified for the request. Details to keep in mind when using
+ * credentials:
*
* * Using credentials is only useful for cross-origin requests.
* * The `Access-Control-Allow-Origin` header of `url` cannot contain a wildcard (*).
@@ -57,9 +71,50 @@ $(ANNOTATIONS)class $CLASSNAME$EXTENDS$IMPLEMENTS$NATIVESPEC {
*
* See also: [authorization headers](http://en.wikipedia.org/wiki/Basic_access_authentication).
*/
- factory $CLASSNAME.getWithCredentials(String url,
- onComplete($CLASSNAME request)) =>
- _HttpRequestUtils.get(url, onComplete, true);
+ static Future<HttpRequest> request(String url,
+ {String method, bool withCredentials, String responseType, sendData,
+ void onProgress(ProgressEvent e)}) {
+ var completer = new Completer<String>();
+
+ var xhr = new HttpRequest();
+ if (method == null) {
+ method = 'GET';
+ }
+ xhr.open(method, url, true);
+
+ if (withCredentials != null) {
+ xhr.withCredentials = withCredentials;
+ }
+
+ if (responseType != null) {
+ xhr.responseType = responseType;
+ }
+
+ if (onProgress != null) {
+ xhr.onProgress.listen(onProgress);
+ }
+
+ xhr.onLoad.listen((e) {
+ if (xhr.status >= 200 && xhr.status < 300 ||
+ xhr.status == 304 ) {
+ completer.complete(xhr);
+ } else {
+ completer.completeError(e);
+ }
+ });
+
+ xhr.onError.listen((e) {
+ completer.completeError(e);
+ });
+
+ if (sendData != null) {
+ xhr.send(sendData);
+ } else {
+ xhr.send();
+ }
+
+ return completer.future;
+ }
$!MEMBERS
}
« no previous file with comments | « tests/html/xhr_test.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698