Control When Put Vs Post Is Called With Restangular Save Method?
Solution 1:
I checked the Restangular source and found the following:
function save(params, headers) {
if (this[config.restangularFields.fromServer]) {
returnthis[config.restangularFields.put](params, headers);
} else {
return _.bind(elemFunction, this)('post', undefined, params, undefined, headers);
}
}
So, Restangular differs between javascript objects that you created and restangularized, and those you got via a Restangular method (flavors of the get
method). If it is the first it uses POST
, because locally restangularizing an element implies that it doesn't exist in the remote location. After the first save or if you pulled it from a remote source, it will use PUT
.
This behavior fits REST very well, because POST
for creation of objects and PUT
for the updates is an frequently used approach for REST APIs. See this section from Wikipedia on REST for an example.
For your specific situation, there are (at least) following two solutions:
Update the API to allow PUTs
for updates if you have access to it or generally use the post
method of your restangularized elements. I would suggest not to directly change the config.restangularFields.fromServer
field or to override the save
method of Restangular (although both should work).
Post a Comment for "Control When Put Vs Post Is Called With Restangular Save Method?"