2

I'm using an AngularJS $resource factory to access a client REST services. Some of the parameters are defined with optional path parameters rather than query parameters.

Case 1:

I call myUrl/application-:applicationID/limit-:limit.json

with:

{
    applicationID:"1234",
    limit: 10
}

I get the correct URL of myUrl/application-1234/limit-10.json

Case 2:

I call myUrl/application-:applicationID/limit-:limit.json

with:

{
    applicationID:"1234"
}

I want the resulting url to ignore the optional limit parameter and collapse down to myUrl/application-1234.json

However, what I get is myUrl/application-1234/limit-.json

*EDIT I have several API categories eg Foo1, Foo2, and each has several endpoints eg endPoint1, endPoint2 and using the method in @Deiter's post, I can't see a way to cleanly list all my resources without using temporary category names then binding them all together at the end.

MyServices.factory ( 'MyAPI', ['$resource',

function ( $resource ) {

    return {

    Foo1: $resource ( '', 
    {                       
        application: "@applicationID",
        limit: "@limit"
    }, 
    {           
        endPoint1: {
            ...
        },
        endPoint2: { 
            url: "myURL/application-:applicationID/limit-:limit.json",
            method:'GET', 
            isArray:true
        },
    }),     

    Foo2: $resource ( '', 
    {                       
        application: "@applicationID"
    }, 
    {           
        endPoint1: { 
            ...
        },
        endPoint2: { 
            ...
        }
    }),             
}               
}]);    

Example call:

MyAPI.Foo1.endPoint1 ( { application: "1", limit: 5 } ); 
MyAPI.Foo2.endPoint2 ( { application: "1" } ); 
user3279701
  • 21
  • 1
  • 3
  • Possible duplicate of [optional-url-parameters-in-angularjs-resource](http://stackoverflow.com/questions/10382337/optional-url-parameters-in-angularjs-resource) – Dieterg Feb 06 '14 at 13:49
  • I've edited my original post to show my current structure as I'm not sure how to cleanly integrate the method in your post. – user3279701 Feb 06 '14 at 14:54

1 Answers1

1

You could use the related answer given in the comments by @Dieter, and create separate $resources, but I would suggest simply making each piece of the URL consistent. Do this by either:

1) Removing the static component of each variable part (eg. myUrl/1234/10)

2) Passing the actual value to the URL that you want to use, and keeping the entire part variable (eg. myUrl/:applicationID/:limit, and pass in application-1234 & limit-10)

Matt Way
  • 32,319
  • 10
  • 79
  • 85
  • I've edited my original post to show the current structure. If I understand you correctly, you suggest that I pass in the key and value pair in the string parameter rather than just the value? – user3279701 Feb 06 '14 at 14:57
  • Yeah. But I also feel like the `application-` and `limit-` are quite redundant. – Matt Way Feb 07 '14 at 01:47
  • Thanks. I agree with you about the url structure but there are quite a lot of endpoints and it's already been deployed on a large multi-platform site, so unfortunately it's very unlikely they will change it. – user3279701 Feb 07 '14 at 16:13