Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular resource overwrite url not working

The documentation here says that:

url – {string} – action specific url override. 
The url templating is supported just like for the resource-level urls.

I would like to use this good feature, I tried this:

angular.module("experience", ['ngResource'])

    .factory('Experience', function ($resource, identity) {

    return $resource("api/experiences/:id", {}, {
        queryAll : {
            method  : 'GET',
            url     : 'api/companies/' + identity.id + '/experiences',
            isArray : true
        }
    });
});

You see I am trying to overwrite the url for queryAll method. but this does not work, the query still sends the url api/experiences. is this really supported or I am doing somehting wrong? thanks for any help.

like image 297
bingjie2680 Avatar asked Sep 16 '13 09:09

bingjie2680


2 Answers

I have a very similar problem in my project. The url couldn't be overridden in resource actions. I am using Angular 1.2.0 and it should support the feature since 1.1.4. So I checked the Angular API reference, CHANGELOG, but no luck. Then I dug into the source code and noticed that the logic of overriding url isn't there. That's the root cause of the problem. I updated Angular to 1.2.0, but I forgot to update angular-resource.js to the corresponding version.

So, long story short: check the version of angular-resource.js

like image 161
Ethan Wu Avatar answered Oct 01 '22 18:10

Ethan Wu


I am not sure if you cann access properties of identity in the url override, but you can try something like this:

return $resource("api/experiences/:id", {}, {
    queryAll : {
        method  : 'GET',
        url     : 'api/companies/:id/experiences',
        params  : { id: identity.id },
        isArray : true
    }
});

The id: identity.id tells angular to use the id property of identity, (if it exists).

like image 26
kleinph Avatar answered Oct 01 '22 18:10

kleinph