[javascript] What's the AngularJS “way” of handling a CRUD resource



Answers

The angular way is the restful way:

GET all http://example.com/athletes
GET one http://example.com/athletes/1
POST new http://example.com/athletes
PUT edit http://example.com/athletes/1
DELETE remove http://example.com/athletes/1

Note that $resource also expects a few other things, like resource URLs not ending with a slash, PUT requests returning the updated resource, etc.

If your API doesn't meet these criteria, or you simply need more flexibility, you can build your own $resource-like CRUD service based on the lower-level $http service. One way of doing the latter is explained here

Question

I am interested in moving a lot of my client's "logic" away from Rails routing to AngularJS. I have slight confusion in one topic and that is linking. Now, I do understand there's more than one way to handle this, but what is the common practice in the AngularJS community for handling URLs on handling CRUD for resources. Imagine in the case of an athlete we have a URL such as the following to list all athletes:

http://example.com/athletes

To view an individual athlete:

http://example.com/athletes/1

To edit an individual athlete:

http://example.com/athletes/1/edit

To create a new athlete:

http://example.com/athletes/new

In AngularJS, is it common practice to reroute to similar URLs to create/edit/update? Would you just have one URL handle all of the CRUD type actions in one interface and never change the URL? If you were to change the URL, does that get handled via ng-click and in the click event would you use the $location object to change URLs? I'd love to be able to read up on common practices such as these, but having a difficult time in finding more recent literature on it in an AngularJS context.

** NOTE **

I totally get that you can still use RESTful routes to the backend in order to interact with server-side resources. My question is, what is the style that is recommended to use when updating URLs on the client-side. Are you using AngularJS to do that for each of the CRUD operations?




In AngularJS you can definitely use RESTful server side data sources, there is build in service called $resource.

Alternatively you can also use restangular which has additional features over $resource.

If you want to have full control you can always use $http service which is low level angular component for interacting with http.




Related