Last active
February 1, 2016 14:02
-
-
Save Foxandxss/b6139d9d668c6ea3c673 to your computer and use it in GitHub Desktop.
A backend-less file for plunker
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
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