From 10e1c759f4602d993a76b0eacf6a2d04c8880017 Mon Sep 17 00:00:00 2001 From: petrovalex Date: Fri, 11 May 2012 23:14:59 +0300 Subject: fix($resource): ignore undefined parameters - $resource should handle multiple params with same name - ignore slashes of undefined parameters - fix default parameters issue, mentioned in #875 Closes #875 Closes #782 --- src/ngResource/resource.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'src/ngResource') diff --git a/src/ngResource/resource.js b/src/ngResource/resource.js index 166fc9b8..cd03d2d2 100644 --- a/src/ngResource/resource.js +++ b/src/ngResource/resource.js @@ -280,12 +280,17 @@ angular.module('ngResource', ['ng']). url: function(params) { var self = this, url = this.template, + val, encodedVal; params = params || {}; forEach(this.urlParams, function(_, urlParam){ - encodedVal = encodeUriSegment(params[urlParam] || self.defaults[urlParam] || ""); - url = url.replace(new RegExp(":" + urlParam + "(\\W)"), encodedVal + "$1"); + if (val = (params[urlParam] || self.defaults[urlParam])) { + encodedVal = encodeUriSegment(val); + url = url.replace(new RegExp(":" + urlParam + "(\\W)", "g"), encodedVal + "$1"); + } else { + url = url.replace(new RegExp("/?:" + urlParam + "(\\W)", "g"), '$1'); + } }); url = url.replace(/\/?#$/, ''); var query = []; @@ -306,8 +311,9 @@ angular.module('ngResource', ['ng']). actions = extend({}, DEFAULT_ACTIONS, actions); - function extractParams(data){ + function extractParams(data, actionParams){ var ids = {}; + paramDefaults = extend(paramDefaults, actionParams); forEach(paramDefaults || {}, function(value, key){ ids[key] = value.charAt && value.charAt(0) == '@' ? getter(data, value.substr(1)) : value; }); @@ -362,7 +368,7 @@ angular.module('ngResource', ['ng']). var value = this instanceof Resource ? this : (action.isArray ? [] : new Resource(data)); $http({ method: action.method, - url: route.url(extend({}, extractParams(data), action.params || {}, params)), + url: route.url(extend({}, extractParams(data, action.params || {}), params)), data: data, headers: extend({}, action.headers || {}) }).then(function(response) { -- cgit v1.2.3