I'm trying to make a delay inside of $httpBackend
response, and handle it with $resource
service like this:
$httpBackend.whenGET(/\/courses\/\?id=\d+$/).respond(function(method, url) {
var regexp = /\d+$/;
var position = url.search(regexp);
var course = url.slice(position);
var courseToRet = angular.toJson(getCourse(course));
var defer = $q.defer();
$timeout(function() {
defer.resolve(courseToRet);
}, 500);
return [200, defer.promise, {}];
});
And my $resource
looks like this:
var resource = $resource('/courses/', null, {
'update': {method: 'PUT'}
});
function getCourseResource(courseId) {
return resource.get({id: courseId}).$promise;
}
In my controller I'm trying to get the data like this:
Course.getCourseResource(self.courseId).then(function (result) {
console.log(result);
result.$promise.then(function(data) {
console.log(data);
data.$promise.then(function(data2) {
console.log(data2)
});
});
})
But it seems like it doesn't work. I'm out of ideas. In training purposes I have to do this in that exact way: by using $q
in $httpBackend
to make delay and get data by $resource
. Thank you!