mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-05-28 15:28:15 +00:00
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
This commit is contained in:
parent
6c67719dfa
commit
10e1c759f4
2 changed files with 35 additions and 6 deletions
|
|
@ -280,12 +280,17 @@ angular.module('ngResource', ['ng']).
|
||||||
url: function(params) {
|
url: function(params) {
|
||||||
var self = this,
|
var self = this,
|
||||||
url = this.template,
|
url = this.template,
|
||||||
|
val,
|
||||||
encodedVal;
|
encodedVal;
|
||||||
|
|
||||||
params = params || {};
|
params = params || {};
|
||||||
forEach(this.urlParams, function(_, urlParam){
|
forEach(this.urlParams, function(_, urlParam){
|
||||||
encodedVal = encodeUriSegment(params[urlParam] || self.defaults[urlParam] || "");
|
if (val = (params[urlParam] || self.defaults[urlParam])) {
|
||||||
url = url.replace(new RegExp(":" + urlParam + "(\\W)"), encodedVal + "$1");
|
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(/\/?#$/, '');
|
url = url.replace(/\/?#$/, '');
|
||||||
var query = [];
|
var query = [];
|
||||||
|
|
@ -306,8 +311,9 @@ angular.module('ngResource', ['ng']).
|
||||||
|
|
||||||
actions = extend({}, DEFAULT_ACTIONS, actions);
|
actions = extend({}, DEFAULT_ACTIONS, actions);
|
||||||
|
|
||||||
function extractParams(data){
|
function extractParams(data, actionParams){
|
||||||
var ids = {};
|
var ids = {};
|
||||||
|
paramDefaults = extend(paramDefaults, actionParams);
|
||||||
forEach(paramDefaults || {}, function(value, key){
|
forEach(paramDefaults || {}, function(value, key){
|
||||||
ids[key] = value.charAt && value.charAt(0) == '@' ? getter(data, value.substr(1)) : value;
|
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));
|
var value = this instanceof Resource ? this : (action.isArray ? [] : new Resource(data));
|
||||||
$http({
|
$http({
|
||||||
method: action.method,
|
method: action.method,
|
||||||
url: route.url(extend({}, extractParams(data), action.params || {}, params)),
|
url: route.url(extend({}, extractParams(data, action.params || {}), params)),
|
||||||
data: data,
|
data: data,
|
||||||
headers: extend({}, action.headers || {})
|
headers: extend({}, action.headers || {})
|
||||||
}).then(function(response) {
|
}).then(function(response) {
|
||||||
|
|
|
||||||
|
|
@ -55,12 +55,14 @@ describe("resource", function() {
|
||||||
$httpBackend.expect('GET', '/Path');
|
$httpBackend.expect('GET', '/Path');
|
||||||
$httpBackend.expect('GET', '/Path/1');
|
$httpBackend.expect('GET', '/Path/1');
|
||||||
$httpBackend.expect('GET', '/Path/2/3');
|
$httpBackend.expect('GET', '/Path/2/3');
|
||||||
$httpBackend.expect('GET', '/Path/4/5/6');
|
$httpBackend.expect('GET', '/Path/4/5');
|
||||||
|
$httpBackend.expect('GET', '/Path/6/7/8');
|
||||||
|
|
||||||
R.get({});
|
R.get({});
|
||||||
R.get({a:1});
|
R.get({a:1});
|
||||||
R.get({a:2, b:3});
|
R.get({a:2, b:3});
|
||||||
R.get({a:4, b:5, c:6});
|
R.get({a:4, c:5});
|
||||||
|
R.get({a:6, b:7, c:8});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -123,6 +125,27 @@ describe("resource", function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
it('should build resource with action default param reading the value from instance', function() {
|
||||||
|
$httpBackend.expect('POST', '/Customer/123').respond();
|
||||||
|
var R = $resource('/Customer/:id', {}, {post: {method: 'POST', params: {id: '@id'}}});
|
||||||
|
|
||||||
|
var inst = new R({id:123});
|
||||||
|
expect(inst.id).toBe(123);
|
||||||
|
|
||||||
|
inst.$post();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
it('should handle multiple params with same name', function() {
|
||||||
|
var R = $resource('/:id/:id');
|
||||||
|
|
||||||
|
$httpBackend.when('GET').respond('{}');
|
||||||
|
$httpBackend.expect('GET', '/1/1');
|
||||||
|
|
||||||
|
R.get({id:1});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
it("should create resource", function() {
|
it("should create resource", function() {
|
||||||
$httpBackend.expect('POST', '/CreditCard', '{"name":"misko"}').respond({id: 123, name: 'misko'});
|
$httpBackend.expect('POST', '/CreditCard', '{"name":"misko"}').respond({id: 123, name: 'misko'});
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue