AngularJS best practice REST / CRUD AngularJS best practice REST / CRUD angularjs angularjs

AngularJS best practice REST / CRUD


There is no Angular prescribed way for what you are asking. It's up to you to determine the implementation detail.

Typically I only use two controllers and templates per resource:

  1. ListController
  2. FormController

The Form controller is used for both Edit and Create operations. Use the resolve option in your route definitions to pass in either User.get() or User.new() and a flag indicating if this is an edit or create operation. This flag can then be used inside your FormController to decide which save method to call. Here's a simple example:

.when( '/users', {  templateUrl: BASE + 'partials/user-list.html',  controller: 'UserListCtrl' } ).when( '/user/new', {  templateUrl: BASE + 'partials/user-form.html',  resolve: {    data: ['User', function(User) { return User.new(); }],    operation: 'create'  }  controller: 'UserFormCtrl' } ).when( '/user/:userId', {  templateUrl: BASE + 'partials/user-form.html',  resolve: {    data: ['User', '$route', function(User, $route) { return User.get($route.current.params.userId); }],    operation: 'edit'  }  controller: 'UserFormCtrl' } )

And your form controller:

app.controller('UserFormCtrl', ['$scope', 'data', 'operation', function($scope, data, operation){  $scope.data = data;  $scope.save = function() {    if (operation === 'edit') {      // Do you edit save stuff    } else {      // Do you create save stuff    }  }}]);

You can go a step further and create a base list and form controller to move stuff like error handling, server-side validation notifications etc. In fact for the majority of CRUD operations you can even move the save logic to this base controller.


My research into a similar quest has lead me to this project "angular-schema-form" https://github.com/Textalk/angular-schema-form.

For this approach...You make a JSON-Schema that describes your data. Then augment it with another little JSON-struct that describes a "form" (ie. view specific info that does not belong in the data schema) and it makes a UI (form) for you.

One cool advantage is that the Schema is also useful in validating the data (client and server side), so that is a bonus.

You have to figure out what events should fire off GET/POST/... back to your API. but that would be your preference, eg. Hit the API for every key stroke OR the classic [Submit] button POST back style OR something in between with a timed Auto Save.

To keep this idea going, I think that it is possible to use StrongLoop to make a quick API, which (again) uses your data's schema (augmented with some storage details) to define the API.

no <3 uses of that schema, see [http://json-schema.org/] which is central to this approach.

(read: no less than three :)


You maybe mixing things up. CRUD operations at API level are done using $resource and these may or may not map to UI.So using $resouce if you define resource as

var r = $resource('/users/:id',null,   {'update': { method:'PUT' }});r.query()  //does GET on /users and gets all usersr.get({id:1}) // does GET on /users/1 and gets a specific userr.save(userObject)  // does a POST to /users to save the userr.update({ id:1 }, userObject) // Not defined by default but does PUT to /users/1 with user object.

As you see the API is resource full but is in no way linked to any UI view.

For view you can use the convention you have defined, but nothing specific is provided by Angular.