角JS:GET / POST /删除/ PUT客户端的完整的示例为REST / CRUD后端? [英] Angular JS: Full example of GET/POST/DELETE/PUT client for a REST/CRUD backend?

查看:160
本文介绍了角JS:GET / POST /删除/ PUT客户端的完整的示例为REST / CRUD后端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用这篇文章作为例子以下实现的REST / CRUD后端:<一href=\"http://coenraets.org/blog/2012/10/creating-a-rest-api-using-node-js-ex$p$pss-and-mongodb/\">http://coenraets.org/blog/2012/10/creating-a-rest-api-using-node-js-ex$p$pss-and-mongodb/ 。我已经MongoDB的本地运行,我没有使用MongoLabs。

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.

我已经按照使用ngResource的谷歌教程和工厂模式,我有查询(GET的所有项目),得到一个项目(GET),创建一个项目(POST),并删除一个项目(DELETE)工作。我有困难,实现PUT的方式后端API希望它 - 一个PUT到包含ID的URL(... /富/)外,还包括更新的数据

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.

我有code的此位来定义我的服务:

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'}
    });

我把从该控制器code中的方法:

I call the method from this controller code:

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

但是当我调用更新功能,URL不包括ID值:这只是/ realmen,而不是/ realmen / ID

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

我试过涉及增加一个RealMen.prototype.update的各种解决方案,但仍不能得到ENTRYID露面的URL。 (它也像我不得不打造的JSON拿着刚刚数据库字段值自己 - 创建一个新的条目时POST操作做它自动为我,但似乎没有成为一个数据结构,只包含当我查看/编辑单个条目)字段值。

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).

有没有使用所有四个动词预期RESTful方式的例子客户端应用程序?

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

我也看到了,它覆盖$保存这么Restangular和另一种解决方案的参考,它可以发出POST或PUT(<一个href=\"http://kirkbushell.me/angular-js-using-ng-resource-in-a-more-restful-manner/\">http://kirkbushell.me/angular-js-using-ng-resource-in-a-more-restful-manner/).这种技术似乎正在改变如此之快,似乎没有成为一个很好的参考解决方案,人可以作为一个例子使用。

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.

推荐答案

我Restangular的创造者。

I'm the creator of Restangular.

您可以在这个CRUD例子看看,看看如何可以把/ POST / GET元素,而你需要做的所有的URL配置和$资源配置。除了它,然后你可以使用嵌套的资源,无需任何配置。)

You can take a look at this CRUD example to see how you can PUT/POST/GET elements without all that URL configuration and $resource configuration that you need to do. Besides it, you can then use nested resources without any configuration :).

看看这个plunkr例如:

Check out this plunkr example:

http://plnkr.co/edit/d6yDka?p=$p$pview

您也可以看到README和检查这里的文档 https://github.com/mgonto/restangular

You could also see the README and check the documentation here https://github.com/mgonto/restangular

如果你需要一些功能,这是不存在的,只是创建的问题。我通常添加一个星期内问功能,我也使用这个库为我所有的AngularJS项目:)

If you need some feature that's not there, just create an issue. I usually add features asked within a week, as I also use this library for all my AngularJS projects :)

希望它帮助!

这篇关于角JS:GET / POST /删除/ PUT客户端的完整的示例为REST / CRUD后端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆