Last active
April 22, 2016 19:15
-
-
Save JustMaier/4abe1adb56c79d33ca173f881799cbb1 to your computer and use it in GitHub Desktop.
Handle 201 in angular $resource - Get Id
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Config the $resourceProvider | |
app.config(["$resourceProvider",function ($resourceProvider) { | |
// extend the default actions | |
angular.extend($resourceProvider.defaults.actions,{ | |
save : { | |
method : "POST", | |
interceptor: { | |
response: function(response){ | |
if(response.status == 201){ | |
// Get the id from the location - I'm assuming that the ID is the last part of the url. | |
var locationParts = response.headers("Location").split('/'); | |
response.resource.id = locationParts[locationParts.length - 1]; | |
// Alternatively you can return an Id header and just use that | |
// response.resource.id = response.headers("Id"); | |
} | |
return response.resource; | |
} | |
} | |
} | |
}); | |
}]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment