AngularJS Restful Routing

后端 未结 4 1387
滥情空心
滥情空心 2021-01-05 02:44

I\'m trying to structure my app using the Restful/Ruby convension //[method]/[id]. How I\'ve done it previously when using a server-side MVC fra

4条回答
  •  离开以前
    2021-01-05 03:14

    Thinking about this a little more. You could just have a single controller for those generic CRUD/REST type operations. Then load the templates using the resource and view parameters.

    • Create
      • #/foo/create/0
      • This has it's own form template "/views/foo/create.html" and the 0 os just there for a placeholder.
      • on submit you would call a method on the controller ng-click="save()" which would post to the server at POST "/rest/foo".
    • Read
      • #/foo/view/1
      • Again the template "/views/foo/view.html" is just a view of the data
      • You can call a service method to get the data from your server using GET "/rest/foo/1"
    • Update -#/foo/edit/1
      • Could use the same template as create or you could use a different one "/views/foo/edit.html" if you like.
      • Also pull the data using GET "/rest/foo/1"
      • Submit the data using PUT "/rest/foo/1"
    • Delete
      • #/foo/delete/1
      • service method would call DELETE "/rest/foo/1"
      • I don't think you want a hash for this, but you could use one because the controller could actually do a verification or anything you like to confirm the deletion. Maybe have a view called "/views/foo/delete.html" that asks if you want to delete the record. Then you could have ng-click="delete(itemid)" on a button somewhere that deletes the item via ajax.

    All this could be done using a single controller/service and dynamically generating the service and view urls.

    Anything that's custom you would need a custom controller and custom routes and service methods for. I could probably throw together an example, but not tonight.

提交回复
热议问题