0

I have sample code in meanjs, i don't understand why they must set third paprameter

  update: {
    method: 'PUT'
  }

This is full code:

'use strict';

//Articles service used for communicating with the articles REST endpoints
angular.module('articles').factory('Articles', ['$resource',
  function ($resource) {
    return $resource('api/articles/:articleId', {
      articleId: '@_id'
    }, {
      update: {
        method: 'PUT'
      }
    });
  }
]);

Thanks in advance.

phan ngoc
  • 191
  • 3
  • 14

1 Answers1

2

If you take a look at the docs specifically under the Returns section you'll see that the $resource service will return:

A resource "class" object with methods for the default set of resource actions optionally extended with custom actions. The default set contains these actions:

{'get':    {method:'GET'},
 'save':   {method:'POST'},
 'query':  {method:'GET', isArray:true},
 'remove': {method:'DELETE'},
 'delete': {method:'DELETE'} };

It further states:

The actions save, remove and delete are available on it as methods with the $ prefix.

So $save, $remove, $delete are avaiable but no $update. This is why the service in the example has the line:

...
'update': { method: 'PUT'},
...

It's meant to extend these default set of actions so that $update will be available as a method on the objects and it will use the HTTP PUT method instead of GET/POST/DELETE like the others.

Note: the above answer was extracted from a previous question I answered but I've isolated the part you should focus on here

Community
  • 1
  • 1
jsonmurphy
  • 1,600
  • 1
  • 11
  • 19
  • You could also use **PATCH** instead of **PUT** if you are doing partial updates to the object to save on bandwidth. [RFC 5789](http://tools.ietf.org/html/rfc5789) – causztic Nov 25 '15 at 02:55
  • Thank you very much, it very usefull to me :) – phan ngoc Nov 25 '15 at 13:05