AngularJS resource optional path parameters

903 views
Skip to first unread message

mcoupar

unread,
Feb 7, 2014, 12:39:56 PM2/7/14
to ang...@googlegroups.com
I'm using an AngularJS $resource factory to access some existing REST services. Some of the parameters are defined with optional path parameters rather than query parameters.

Examples:

MyAPI.Foo1.endPoint2 ( { application: "1234", limit: 5 } ); 
MyAPI.Foo1.endPoint2 ( { application: "1234" } ); 

What I want is to be able to use the same resource with optional parameters eg I want the resulting url to ignore the optional limit parameter if it's not specified and collapse down to "myUrl/application-1234.json".  However, what I get in the second example is "myUrl/application-1234/limit-.json".

I have several API categories eg Foo1, Foo2, and each has several endpoints eg endPoint1, endPoint2,

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: { 
            ...
        }
    }),             
}               
}]);    

I've tried the method detailed on this StackOverflow post [http://stackoverflow.com/questions/10382337/optional-url-parameters-in-angularjs-resource] but I simply can't get it to work where both the following examples give the correct url.  

Am I doing it wrong or is what I'm asking just not doable?
Reply all
Reply to author
Forward
0 new messages