0

So, I have a REST resource along these lines...

/api/Dogs
/api/Dogs/1
/api/Dogs/2
...

Where a Dog looks like...

{
    Id: 1,
    Url: "http://kennelclub/api/Dogs/1",
    Name: "Butch"
}

and a second resource like so...

/api/Owners
/api/Owners/1
....

As a convenience, to get the dogs for an owner I have this method of querying...

/api/Owners/1/Dogs/

Now, it's trivially easy to get an AngularJS $resource to read an owners dogs, as follows...

var ownersDogs = $resource("/api/Owners/:ownerId/Dogs/:dogId", { dogId: "@Id" });
var ownersDogs.query({ ownerId: 1 });

Finally, a Question

I want to make a change to a dog obtained via this URL with magic angular $save function...

ownersDogs[0].Name = "Barry";
ownersDogs[0].$save();

However, there are two problems here...

  • this convenience resource url is read only (i.e. doesn't accept PUT/POST).
  • Even if it wasn't, I need to re-supply the ownerId to the $get/$save functions on the objects to work correctly.

If I want to edit the dog returned, I need to use the /api/Dogs/1 url. This both allows read/write, and doesn't require an ownerId. You'll notice that this Url is already embedded in the Dog object returned.

Is there some way I can get the $save, $get, etc. functions on the returned object to automatically(?) use the Url embedded in the resource? Or at least, is there some way to change the URL that $save will use?

3
  • In my opinion when i comes to be too complicated in term of URL i prefer to use my url with $http calls exposing them in a service instead of using $resource. But that's just a matter or tastes. Do you really want to keep up with $resource ? Commented Jul 17, 2015 at 12:42
  • $resource is a lot cleaner in my opinion. I particularly like the way it returns the object that will be used immediately, and then fills in the object later without the need for any callbacks. Commented Jul 17, 2015 at 13:19
  • Yeah i definitely agree with you about this. Kenavoz gave a pretty nice answer though. Commented Jul 17, 2015 at 13:44

3 Answers 3

2

You can add custom methods to your resource with custom url :

var ownersDogs = $resource("/api/Owners/:ownerId/Dogs/:dogId", { dogId: "@Id" },
    {
    'savedog': { method : 'PUT', url: '/api/Dogs/:dogId'}
    }
);

Calling it with :

ownersDogs[0].$savedog();
Sign up to request clarification or add additional context in comments.

Comments

0

You can parametrize the $save in the following way:

ownerDogs[0].$save(function(ownerObject, putResponseHeaders) {
    //ownerObject => saved user object
    //putResponseHeaders => $http header getter
});

Comments

0

I can't find a way to make use of the Url parameter in the returned resource, but I solved the problem in a different way. You can simply specify different URLs for the save, etc. operations as follows...

var ownersDogs = $resource("/api/Owners/:ownerId/Dogs/:dogId", { dogId: "@Id" },
    {
        'save': { method: 'POST', url: '/api/Dogs/:dogId'}
    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.