57

I've implemented a REST/CRUD backend by following this article as an example: http://coenraets.org/blog/2012/10/creating-a-rest-api-using-node-js-express-and-mongodb/ . I have MongoDB running locally, I'm not using MongoLabs.

I've followed the Google tutorial that uses ngResource and a Factory pattern and I have query (GET all items), get an item (GET), create an item (POST), and delete an item (DELETE) working. I'm having difficulty implementing PUT the way the backend API wants it -- a PUT to a URL that includes the id (.../foo/) and also includes the updated data.

I have this bit of code to define my services:

angular.module('realmenServices', ['ngResource']).
    factory('RealMen', function($resource){
    return $resource('http://localhost\\:3000/realmen/:entryId', {}, {
      query: {method:'GET', params:{entryId:''}, isArray:true},
      post: {method:'POST'},
      update: {method:'PUT'},
      remove: {method:'DELETE'}
    });

I call the method from this controller code:

$scope.change = function() {
    RealMen.update({entryId: $scope.entryId}, function() {
            $location.path('/');
    });
}

but when I call the update function, the URL does not include the ID value: it's only "/realmen", not "/realmen/ID".

I've tried various solutions involving adding a "RealMen.prototype.update", but still cannot get the entryId to show up on the URL. (It also looks like I'll have to build the JSON holding just the DB field values myself -- the POST operation does it for me automatically when creating a new entry, but there doesn't seem to be a data structure that only contains the field values when I'm viewing/editing a single entry).

Is there an example client app that uses all four verbs in the expected RESTful way?

I've also seen references to Restangular and another solution that overrides $save so that it can issue either a POST or PUT (http://kirkbushell.me/angular-js-using-ng-resource-in-a-more-restful-manner/). This technology seems to be changing so rapidly that there doesn't seem to be a good reference solution that folks can use as an example.

4

3 に答える 3

60

私は Restangular の作成者です。

この CRUD の例を見て、必要な URL 構成や $resource 構成を一切行わずに要素を PUT/POST/GET できる方法を確認できます。それに加えて、構成なしでネストされたリソースを使用できます:)。

この plunkr の例を確認してください。

http://plnkr.co/edit/d6yDka?p=preview

また、README を参照して、 https://github.com/mgonto/restangularのドキュメントを確認することもできます。

そこにない機能が必要な場合は、問題を作成してください。私はすべてのAngularJSプロジェクトにもこのライブラリを使用しているため、通常は1週間以内に機能を追加します:)

それが役に立てば幸い!

于 2013-06-30T00:00:47.947 に答える
9

この方法で実装できます

$resource('http://localhost\\:3000/realmen/:entryId', {entryId: '@entryId'}, {
        UPDATE: {method: 'PUT', url: 'http://localhost\\:3000/realmen/:entryId' },
        ACTION: {method: 'PUT', url: 'http://localhost\\:3000/realmen/:entryId/action' }
    })

RealMen.query() //GET  /realmen/
RealMen.save({entryId: 1},{post data}) // POST /realmen/1
RealMen.delete({entryId: 1}) //DELETE /realmen/1

//any optional method
RealMen.UPDATE({entryId:1}, {post data}) // PUT /realmen/1

//query string
RealMen.query({name:'john'}) //GET /realmen?name=john

ドキュメント: https://docs.angularjs.org/api/ngResource/service/$resource

それが役に立てば幸い

于 2014-05-08T23:23:18.430 に答える