From 2a2123441c2b749b8f316a24c3ca3f77a9132a01 Mon Sep 17 00:00:00 2001 From: Mark Nadig Date: Wed, 30 Jan 2013 08:41:39 -0700 Subject: fix($resource): params should expand array values properly Today, calling e.g. var R = $resource('/Path/:a'); R.get({a: 'foo', bar: ['baz1', 'baz2']}); results in a query string like "/Path/doh?bar=baz1,baz2" which is undesirable. This commit enhances resource to use $http to encode any non-url parameters resulting in a query string like "/Path/doh?bar=baz1&bar=baz2". BREAKING CHANGE: if the server relied on the buggy behavior then either the backend should be fixed or a simple serialization of the array should be done on the client before calling the resource service. --- src/ngResource/resource.js | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'src/ngResource') diff --git a/src/ngResource/resource.js b/src/ngResource/resource.js index 484e9b01..6e179827 100644 --- a/src/ngResource/resource.js +++ b/src/ngResource/resource.js @@ -316,7 +316,7 @@ angular.module('ngResource', ['ng']). } Route.prototype = { - url: function(params) { + setUrlParams: function(config, params) { var self = this, url = this.template, val, @@ -339,16 +339,17 @@ angular.module('ngResource', ['ng']). }); } }); - url = url.replace(/\/?#$/, ''); - var query = []; + + // set the url + config.url = url.replace(/\/?#$/, '').replace(/\/*$/, ''); + + // set params - delegate param encoding to $http forEach(params, function(value, key){ if (!self.urlParams[key]) { - query.push(encodeUriQuery(key) + '=' + encodeUriQuery(value)); + config.params = config.params || {}; + config.params[key] = value; } }); - query.sort(); - url = url.replace(/\/*$/, ''); - return url + (query.length ? '?' + query.join('&') : ''); } }; @@ -426,7 +427,7 @@ angular.module('ngResource', ['ng']). } }); httpConfig.data = data; - httpConfig.url = route.url(extend({}, extractParams(data, action.params || {}), params)) + route.setUrlParams(httpConfig, extend({}, extractParams(data, action.params || {}), params)); function markResolved() { value.$resolved = true; } -- cgit v1.2.3