A good example of an operation using the AngularJS $ resource

I tried my best to find a consistent and good example of an operation using the AngularJS $ resource. An example of when I want to update, but it doesn't seem to be here: AngularJS PUT on a REST voting app

Basically, I have to understand the best practice / normal way of conducting an input operation both for submitting the form and in the voting application mentioned in my post above. Does anyone have a good example demonstrating put?

+6
source share
1 answer

If you are creating a new object in your data warehouse, you want to use POST / save. If you are updating data associated with an existing entity in the data warehouse, you want to use PUT / update. A patch is usually reserved to simply update a subset of the entity data.

See RFC

Several applications that extend the Hypertext Transfer Protocol (HTTP) require a function to partially modify resources. The existing HTTP PUT Method allows you to completely replace a document. This sentence adds a new HTTP PATCH method to modify an existing HTTP protocol resource.

You must specify the identifier using the PUT and PATCH operations. You will not supply it with a POST operation.

When we load our angular forms, this is done in one of two ways. If the form loads when we create a new object, then we will not have an identifier. We will know this in the controller and will call resource.save. If we provide the controller with a form loading with an identifier that was used to output data from the endpoint to fill out the form, we now have an identifier that we can use to perform resource.update or resource.patch operations, depending on which part of the entity we are updated.

Here is an example of saving a function that handles update and save operations. Here we check if the identifier was sent along the route before we make our call to the resource.

angular.module('appModule').controller('ExampleCtrl', ['$scope', '$routeParams', function($scope, $routeParams) { $scope.saveForm = function () { //Do input validation before you make a resource call if ($routeParams.id) { //call resource update since we have an id } else { //call resource save since we don't have an id } }; }]); 

Here is an example from angularjs documentation:

How to create a custom PUT request:

 var app = angular.module('app', ['ngResource', 'ngRoute']); // Some APIs expect a PUT request in the format URL/object/ID // Here we are creating an 'update' method app.factory('Notes', ['$resource', function($resource) { return $resource('/notes/:id', null, { 'update': { method:'PUT' } }); }]); // In our controller we get the ID from the URL using ngRoute and $routeParams // We pass in $routeParams and our Notes factory along with $scope app.controller('NotesCtrl', ['$scope', '$routeParams', 'Notes', function($scope, $routeParams, Notes) { // First get a note object from the factory var note = Notes.get({ id:$routeParams.id }); $id = note.id; // Now call update passing in the ID first then the object you are updating Notes.update({ id:$id }, note); // This will PUT /notes/ID with the note object in the request payload }]); 
+14
source

Source: https://habr.com/ru/post/981088/


All Articles