AngularJS $资源RESTful示例(AngularJS $resource RESTful example)
我想使用$资源来调用我的RESTful Web服务(我仍在工作),但是我想知道我的AngularJS脚本是否正确。
todo DTO有:
{id, order, content, done}
:cmd
是所以我可以调用api/1/todo/reset
来清除数据库中的todo表。这是代码与我的理解的评论:
function TodoService($resource) { var src = $resource('api/1/todo/:id:cmd', {id: "@id", cmd: "@cmd"}, //parameters default { ListTodos: { method: "GET", params: {} }, GetTodo: { method: "GET", params: { id: 0 } }, CreateTodo: { method: "POST", params: { content: "", order: 0, done: false } }, UpdateTodo: { method: "PATCH", params: { /*...*/ } }, DeleteTodo: { method: "DELETE", params: { id: 0 } }, ResetTodos: { method: "GET", params: { cmd: "reset" } }, }); //Usage: //GET without ID //it calls -> api/1/todo src.ListTodos(); //GET with ID //it calls -> api/1/todo/4 src.GetTodo({ id: 4 }); //POST with content, order, done //it calls -> api/1/todo src.CreateTodo({ content: "learn Javascript", order: 1, done: false }); //UPDATE content only //it calls -> api/1/todo/5 src.UpdateTodo({ id: 5, content: "learn AngularJS" }); //UPDATE done only //it calls -> api/1/todo/5 src.UpdateTodo({ id: 5, done: true }); //RESET with cmd //it calls -> api/1/todo/reset src.ResetTodos(); }
我不确定的一个特别的事情是PATCH方法,我不想更新一切,只能更新一个字段? 我正在构建这段代码吗?
I would like to use $resource to call my RESTful web service, (which I am still working on) but I would like to find out if I got my AngularJS script correct first.
The todo DTO has:
{id, order, content, done}
:cmd
is so I can callapi/1/todo/reset
to clear the todo table in database.Here is the code with comment of my understanding:
function TodoService($resource) { var src = $resource('api/1/todo/:id:cmd', {id: "@id", cmd: "@cmd"}, //parameters default { ListTodos: { method: "GET", params: {} }, GetTodo: { method: "GET", params: { id: 0 } }, CreateTodo: { method: "POST", params: { content: "", order: 0, done: false } }, UpdateTodo: { method: "PATCH", params: { /*...*/ } }, DeleteTodo: { method: "DELETE", params: { id: 0 } }, ResetTodos: { method: "GET", params: { cmd: "reset" } }, }); //Usage: //GET without ID //it calls -> api/1/todo src.ListTodos(); //GET with ID //it calls -> api/1/todo/4 src.GetTodo({ id: 4 }); //POST with content, order, done //it calls -> api/1/todo src.CreateTodo({ content: "learn Javascript", order: 1, done: false }); //UPDATE content only //it calls -> api/1/todo/5 src.UpdateTodo({ id: 5, content: "learn AngularJS" }); //UPDATE done only //it calls -> api/1/todo/5 src.UpdateTodo({ id: 5, done: true }); //RESET with cmd //it calls -> api/1/todo/reset src.ResetTodos(); }
One particular thing I am not sure is the PATCH method, I don't want to update everything, can I update just one field? Am I constructing this piece of code correctly?
原文:https://stackoverflow.com/questions/13269882