diff options
| author | Misko Hevery | 2012-03-23 13:41:48 -0700 |
|---|---|---|
| committer | Misko Hevery | 2012-03-23 14:21:43 -0700 |
| commit | 73c8593077155a9f2e8ef42efd4c497eba0bef4f (patch) | |
| tree | 463c87959a3f6884ec322624dffcc2f4a396a296 /src/service/http.js | |
| parent | ac75079e2113949d5d64adbcf23d56f3cf295d41 (diff) | |
| download | angular.js-73c8593077155a9f2e8ef42efd4c497eba0bef4f.tar.bz2 | |
feat(http): added params parameter
The params parameter can now be used to serialize parameters in the URLs. The serialization does proper escaping and JSON encoding if it is an object.
Diffstat (limited to 'src/service/http.js')
| -rw-r--r-- | src/service/http.js | 32 |
1 files changed, 26 insertions, 6 deletions
diff --git a/src/service/http.js b/src/service/http.js index 17cc68ea..c2cbd161 100644 --- a/src/service/http.js +++ b/src/service/http.js @@ -1,4 +1,5 @@ 'use strict'; +'use strict'; /** * Parse headers into key value object @@ -360,6 +361,8 @@ function $HttpProvider() { * * - **method** – `{string}` – HTTP method (e.g. 'GET', 'POST', etc) * - **url** – `{string}` – Absolute or relative URL of the resource that is being requested. + * - **params** – `{Object.<string|Object>}` – Map of strings or objects which will be turned to + * `?key1=value1&key2=value2` after the url. If the value is not a string, it will be JSONified. * - **data** – `{string|Object}` – Data to be sent as the request message data. * - **headers** – `{Object}` – Map of strings representing HTTP headers to send to the server. * - **transformRequest** – `{function(data, headersGetter)|Array.<function(data, headersGetter)>}` – @@ -638,7 +641,8 @@ function $HttpProvider() { var deferred = $q.defer(), promise = deferred.promise, cache, - cachedResp; + cachedResp, + url = buildUrl(config.url, config.params); $http.pendingRequests.push(config); promise.then(removePendingReq, removePendingReq); @@ -649,7 +653,7 @@ function $HttpProvider() { } if (cache) { - cachedResp = cache.get(config.url); + cachedResp = cache.get(url); if (cachedResp) { if (cachedResp.then) { // cached request has already been sent, but there is no response yet @@ -665,13 +669,13 @@ function $HttpProvider() { } } else { // put the promise for the non-transformed response into cache as a placeholder - cache.put(config.url, promise); + cache.put(url, promise); } } // if we won't have the response in cache, send the request to the backend if (!cachedResp) { - $httpBackend(config.method, config.url, reqData, done, reqHeaders, config.timeout); + $httpBackend(config.method, url, reqData, done, reqHeaders, config.timeout); } return promise; @@ -686,10 +690,10 @@ function $HttpProvider() { function done(status, response, headersString) { if (cache) { if (isSuccess(status)) { - cache.put(config.url, [status, response, parseHeaders(headersString)]); + cache.put(url, [status, response, parseHeaders(headersString)]); } else { // remove promise from the cache - cache.remove(config.url); + cache.remove(url); } } @@ -719,5 +723,21 @@ function $HttpProvider() { if (idx !== -1) $http.pendingRequests.splice(idx, 1); } } + + + function buildUrl(url, params) { + if (!params) return url; + var parts = []; + forEachSorted(params, function(value, key) { + if (value == null || value == undefined) return; + if (isObject(value)) { + value = toJson(value); + } + parts.push(encodeURIComponent(key) + '=' + encodeURIComponent(value)); + }); + return url + ((url.indexOf('?') == -1) ? '?' : '&') + parts.join('&'); + } + + }]; } |
