Well, the $resource service gives you access to the response headers on the
$save call. Personally, I use $http instead of $resource, but you end up
with the same sort of thing in the long run (your headers will be in the
second parameter of the success callback). Within the header, just regex it
and set your $id.

It's altogether possible you can configure your backend to do this
automagically anyway. If I remember correctly, if your POST route returns
the created object (including the appropriate ID field), that data gets
shoved into the instance you're working with anyway. It is, incidentally,
that kind of behavior that encouraged me to just work with $http: i didn't
really need fancy-pants stuff like that, and I wasn't fond of the "magic"
that was happening behind the scenes. Of course, now that I have a better
handle on said magic, I might use ngResource in the future.

You may also want to check out restangular and angular-restmod, two other
all-in-wonder type REST resource modules. Restmod in particular looks
pretty promising IMO.

e


On Thu, Aug 14, 2014 at 2:20 PM, Santiago Basulto <
[email protected]> wrote:

> Hello everyone. I'm using ngResource to build a RESTful app. I've been
> reading a lot and couldn't find the answer to my question. Sorry if it's
> too n00b :)
>
> I'll try to keep this generic so it might be useful to other people.
>
> Suppose you've got a Car resource. The uri is `/api/v1/car/:id/`
>
> To create a resource you POST your (json) data to `/car/` and (if
> successful) returns a 201 CREATED response along with some headers. Among
> those headers there's Location, which has something like this:
> `/api/v1/car/32/`. That way you know that the resource you just created got
> the ID 32. How do you get that "32" (the ID) using ngResource? Please note 
> *you
> don't specify the ID when you're posting*. The server will tell you which
> id it assigned to your resource when it's created (and it'll be returned in
> the Location header).
>
> Here's roughly some code related to my question:
> http://jsfiddle.net/1k36q5qz/
>
> Thank you very much for your help.
>
> --
> You received this message because you are subscribed to the Google Groups
> "AngularJS" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/angular.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to