Skip to content

Instantly share code, notes, and snippets.

@Foxandxss
Last active February 1, 2016 14:02
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Foxandxss/b6139d9d668c6ea3c673 to your computer and use it in GitHub Desktop.
A backend-less file for plunker
angular.module('plunker')
.config(function($provide) {
$provide.decorator('$httpBackend', angular.mock.e2e.$httpBackendDecorator);
})
.run(function($httpBackend) {
var things = [
{
id: 0,
title: 'Finish fake backend',
completed: true
},
{
id: 1,
title: 'Make some cool stuff',
completed: false
},
{
id: 2,
title: 'Brainstorm new projects',
completed: false
}
];
$httpBackend.whenGET('/api/todos').respond(200, things);
$httpBackend.whenPOST('/api/todos').respond(function(method, url, data, headers) {
var newItem = JSON.parse(data);
newItem.id = things.length;
things.push(newItem);
return [201, newItem];
});
$httpBackend.whenPUT(/^\/api\/todos\/\d+$/).respond(function(method, url, data, headers) {
var item = JSON.parse(data);
for (var i = 0, l = things.length; i < l; i++) {
if (things[i].id === item.id) {
things[i] = item;
break;
}
}
return [200, item];
});
$httpBackend.whenDELETE(/^\/api\/todos\/\d+$/).respond(function(method, url, data, headers) {
var regex = /^\/api\/todos\/(\d+)/g;
var id = regex.exec(url)[1]; // First match on the second item.
id = parseInt(id, 10);
for (var i = 0, l = things.length; i < l; i++) {
if (things[i].id === id) {
var index = things.indexOf(things[i]);
things.splice(index, 1);
break;
}
}
return [204];
});
$httpBackend.whenGET(/\.html/).passThrough();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment