Skip to content

Instantly share code, notes, and snippets.

@lazarofl
Created May 30, 2014 19:10
Show Gist options
  • Save lazarofl/b9b8bdf3547071f64249 to your computer and use it in GitHub Desktop.
Save lazarofl/b9b8bdf3547071f64249 to your computer and use it in GitHub Desktop.
$httpBackend para simular as requisições da app de lista de compras
var app = angular.module('app', []);
app.run(function($httpBackend){
$httpBackend.whenGET('/listadecompras').respond(
[ {
nome: 'Pão',
quantidade: 2,
comprado: false
},
{
nome: 'Manteiga',
quantidade: 1,
comprado: false
},
{
nome: 'Leite',
quantidade: 1,
comprado: false
},
{
nome: 'Café',
quantidade: 2,
comprado: false
},
{
nome: 'Açúcar',
quantidade: 1,
comprado: false
}
]
);
this.newGuid = function () {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
s4() + '-' + s4() + s4() + s4();
};
$httpBackend.whenPOST('/listadecompras').respond(function(method, url, data){
var json = angular.fromJson(data);
json.Id = newGuid();
return [200, json];
});
$httpBackend.whenPUT('/listadecompras').respond(function(method, url, data){
var json = angular.fromJson(data);
return [200, json];
});
$httpBackend.whenDELETE('/listadecompras').respond(function(method, url, data){
return [200,{}];
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment