mgonto / restangular

AngularJS service to handle Rest API Restful Resources properly and easily
MIT License
7.87k stars 840 forks source link

Suggestion for CRUD methods on the collection. #616

Open CMCDragonkai opened 10 years ago

CMCDragonkai commented 10 years ago

I've read that the methods PUT, PATCH, DELETE does not exist on the collection object.

In the docs: https://github.com/mgonto/restangular#how-do-i-handle-crud-operations-in-a-list-returned-by-restangular

It demonstrates to get the collection first then work on the elements returned by that collection.

Another way is to instantiate a new object directly from Restangular by using Restangular.one('resource', id).

Both ways I feel is a bit inefficient. The first is in the 2 requests to do one thing, and the second while useful, doesn't allow me to share a single collection object.

What I'd really like to do is something like:

var collection = Restangular.all('accounts');
collection.get(4); //this already works
collection.remove(4); //this doesnt exist
collection.put(4, payload);
collection.patch(4, payload);
//...etc

The docs already state that you get an item off the collection by the id. Why not add all the other methods for consistency?

I know that remove currently exists, but not in a way that makes the above possible. Perhaps a bit more polymorphism on the functions that already exist would be suitable.

mgonto commented 10 years ago

@CMCDragonkai I think that's a good idea.

Marking as enhancement.

Thanks for the contribution

CMCDragonkai commented 10 years ago

Awesome. Can't wait to see it implemented.