In my application, I utilize a custom $resource service to manage CRUD operations for entities over HTTP. The service is simply wrapped in a custom service without additional functionality:
app.factory('coursesService', ['$resource', coursesService]);
function coursesService($resource) {
return $resource('/api/courses/:id', {
id: '@id'
}, {
update: {
method: 'PUT'
}
});
Attempting to delete an instance in the controller (stored in courseToDelete
):
this.courseToDelete.$remove()
To simulate backend behavior, I make use of the $httpBackend service from the ngMockE2E module:
$httpBackend.whenDELETE(/api\/courses\/(.+)/, undefined, undefined, ['id'])
.respond(function(method, url, data, headers, params) {
clientStorage.deleteCourse(params.id);
return [200, {}];
});
When a DELETE request is sent via $resource, I intend to remove it from local storage (as there is no actual backend, data is stored as serialized JSON):
The object being deleted:
{
id: 1,
name: 'Course 1',
duration: 380,
description: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.' +
' Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.' +
' Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur' +
' sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.',
created: new Date(2011,10,30),
authors: [
'Пушкин', 'Лермонтов'
]
}
Although all other parameters appear correct, when the function runs, the params
parameter is unexpectedly undefined instead of containing the expected id
property. Any insights on what might be incorrect in this setup?