|
32 | 32 | * |
33 | 33 | * @returns {Observable} An observable sequence containing the XMLHttpRequest. |
34 | 34 | */ |
35 | | - ajax.ajaxCold = function (settings) { |
| 35 | + var ajaxRequest = ajax.ajax = function (settings) { |
36 | 36 | return new AnonymousObservable(function (observer) { |
37 | | - var isDone = false; |
38 | | - if (typeof settings === 'string') { |
39 | | - settings = { method: 'GET', url: settings, async: true }; |
40 | | - } |
41 | | - settings.method || (settings.method = 'GET'); |
42 | | - if (settings.async === undefined) { |
43 | | - settings.async = true; |
44 | | - } |
| 37 | + var isDone = false; |
| 38 | + if (typeof settings === 'string') { |
| 39 | + settings = { method: 'GET', url: settings, async: true }; |
| 40 | + } |
| 41 | + settings.method || (settings.method = 'GET'); |
| 42 | + if (settings.async === undefined) { |
| 43 | + settings.async = true; |
| 44 | + } |
45 | 45 |
|
46 | | - var xhr; |
47 | | - try { |
48 | | - xhr = getXMLHttpRequest(); |
49 | | - } catch (err) { |
50 | | - observer.onError(err); |
| 46 | + var xhr; |
| 47 | + try { |
| 48 | + xhr = getXMLHttpRequest(); |
| 49 | + } catch (err) { |
| 50 | + observer.onError(err); |
| 51 | + } |
| 52 | + |
| 53 | + try { |
| 54 | + if (settings.user) { |
| 55 | + xhr.open(settings.method, settings.url, settings.async, settings.user, settings.password); |
| 56 | + } else { |
| 57 | + xhr.open(settings.method, settings.url, settings.async); |
51 | 58 | } |
52 | 59 |
|
53 | | - try { |
54 | | - if (settings.user) { |
55 | | - xhr.open(settings.method, settings.url, settings.async, settings.user, settings.password); |
56 | | - } else { |
57 | | - xhr.open(settings.method, settings.url, settings.async); |
| 60 | + if (settings.headers) { |
| 61 | + var headers = settings.headers; |
| 62 | + for (var header in headers) { |
| 63 | + if (hasOwnProperty.call(headers, header)) { |
| 64 | + xhr.setRequestHeader(header, headers[header]); |
58 | 65 | } |
| 66 | + } |
| 67 | + } |
59 | 68 |
|
60 | | - if (settings.headers) { |
61 | | - var headers = settings.headers; |
62 | | - for (var header in headers) { |
63 | | - if (hasOwnProperty.call(headers, header)) { |
64 | | - xhr.setRequestHeader(header, headers[header]); |
65 | | - } |
66 | | - } |
| 69 | + xhr.onreadystatechange = xhr.onload = function () { |
| 70 | + if (xhr.readyState === 4) { |
| 71 | + var status = xhr.status; |
| 72 | + if ((status >= 200 && status <= 300) || status === 0 || status === '') { |
| 73 | + observer.onNext(xhr); |
| 74 | + observer.onCompleted(); |
| 75 | + } else { |
| 76 | + observer.onError(xhr); |
67 | 77 | } |
68 | 78 |
|
69 | | - xhr.onreadystatechange = xhr.onload = function () { |
70 | | - if (xhr.readyState === 4) { |
71 | | - var status = xhr.status; |
72 | | - if ((status >= 200 && status <= 300) || status === 0 || status === '') { |
73 | | - observer.onNext(xhr); |
74 | | - observer.onCompleted(); |
75 | | - } else { |
76 | | - observer.onError(xhr); |
77 | | - } |
78 | | - |
79 | | - isDone = true; |
80 | | - } |
81 | | - }; |
| 79 | + isDone = true; |
| 80 | + } |
| 81 | + }; |
82 | 82 |
|
83 | | - xhr.onerror = function () { |
84 | | - observer.onError(xhr); |
85 | | - }; |
| 83 | + xhr.onerror = function () { |
| 84 | + observer.onError(xhr); |
| 85 | + }; |
86 | 86 |
|
87 | | - xhr.send(settings.body || null); |
88 | | - } catch (e) { |
89 | | - observer.onError(e); |
90 | | - } |
91 | | - |
92 | | - return disposableCreate( function () { |
93 | | - if (!isDone && xhr.readyState !== 4) { |
94 | | - xhr.abort(); |
95 | | - } |
96 | | - }); |
| 87 | + xhr.send(settings.body || null); |
| 88 | + } catch (e) { |
| 89 | + observer.onError(e); |
| 90 | + } |
| 91 | + |
| 92 | + return function () { |
| 93 | + if (!isDone && xhr.readyState !== 4) { xhr.abort(); } |
| 94 | + }; |
97 | 95 | }); |
98 | 96 | }; |
99 | 97 |
|
100 | | - /** @private */ |
101 | | - var ajaxCold = ajax.ajaxCold; |
102 | | - |
103 | | - /** |
104 | | - * Creates a hot observable for an Ajax request with either a settings object with url, headers, etc or a string for a URL. |
105 | | - * |
106 | | - * @example |
107 | | - * source = Rx.DOM.Request.ajax('/products'); |
108 | | - * source = Rx.DOM.Request.ajax( url: 'products', method: 'GET' }); |
109 | | - * |
110 | | - * @param {Object} settings Can be one of the following: |
111 | | - * |
112 | | - * A string of the URL to make the Ajax call. |
113 | | - * An object with the following properties |
114 | | - * - url: URL of the request |
115 | | - * - method: Method of the request, such as GET, POST, PUT, PATCH, DELETE |
116 | | - * - async: Whether the request is async |
117 | | - * - headers: Optional headers |
118 | | - * |
119 | | - * @returns {Observable} An observable sequence containing the XMLHttpRequest. |
120 | | - */ |
121 | | - var observableAjax = ajax.ajax = function (settings) { |
122 | | - return ajaxCold(settings).publishLast().refCount(); |
123 | | - }; |
124 | | - |
125 | 98 | /** |
126 | | - * Creates a cold observable sequence from an Ajax POST Request with the body. |
127 | | - * |
128 | | - * @param {String} url The URL to POST |
129 | | - * @param {Object} body The body to POST |
130 | | - * @returns {Observable} The observable sequence which contains the response from the Ajax POST. |
131 | | - */ |
132 | | - ajax.postCold = function (url, body) { |
133 | | - return observableAjax({ url: url, body: body, method: 'POST', async: true }); |
134 | | - }; |
135 | | - |
136 | | - /** |
137 | | - * Creates a hot observable sequence from an Ajax POST Request with the body. |
| 99 | + * Creates an observable sequence from an Ajax POST Request with the body. |
138 | 100 | * |
139 | 101 | * @param {String} url The URL to POST |
140 | 102 | * @param {Object} body The body to POST |
141 | 103 | * @returns {Observable} The observable sequence which contains the response from the Ajax POST. |
142 | 104 | */ |
143 | 105 | ajax.post = function (url, body) { |
144 | | - return observableAjax({ url: url, body: body, method: 'POST', async: true }); |
| 106 | + return ajaxRequest({ url: url, body: body, method: 'POST', async: true }); |
145 | 107 | }; |
146 | 108 |
|
147 | 109 | /** |
|
151 | 113 | * @returns {Observable} The observable sequence which contains the response from the Ajax GET. |
152 | 114 | */ |
153 | 115 | var observableGet = ajax.get = function (url) { |
154 | | - return observableAjax({ url: url, method: 'GET', async: true }); |
| 116 | + return ajaxRequest({ url: url, method: 'GET', async: true }); |
155 | 117 | }; |
156 | 118 |
|
157 | | - /** |
158 | | - * Creates an observable sequence from an Ajax GET Request with the body. |
159 | | - * |
160 | | - * @param {String} url The URL to GET |
161 | | - * @returns {Observable} The observable sequence which contains the response from the Ajax GET. |
162 | | - */ |
163 | | - var observableGetCold = ajax.getCold = function (url) { |
164 | | - return observableAjax({ url: url, method: 'GET', async: true }); |
165 | | - }; |
166 | 119 |
|
167 | 120 | if (typeof JSON !== 'undefined' && typeof JSON.parse === 'function') { |
168 | 121 | /** |
|
172 | 125 | * @returns {Observable} The observable sequence which contains the parsed JSON. |
173 | 126 | */ |
174 | 127 | ajax.getJSON = function (url) { |
175 | | - return observableGet(url).select(function (xhr) { |
176 | | - return JSON.parse(xhr.responseText); |
177 | | - }); |
178 | | - }; |
179 | | - |
180 | | - /** |
181 | | - * Creates an observable sequence from JSON from an Ajax request |
182 | | - * |
183 | | - * @param {String} url The URL to GET |
184 | | - * @returns {Observable} The observable sequence which contains the parsed JSON. |
185 | | - */ |
186 | | - ajax.getJSONCold = function (url) { |
187 | | - return observableGetCold(url).select(function (xhr) { |
| 128 | + return observableGet(url).map(function (xhr) { |
188 | 129 | return JSON.parse(xhr.responseText); |
189 | 130 | }); |
190 | 131 | }; |
|
0 commit comments