Skip to content

Instantly share code, notes, and snippets.

@washingtonsoares
Created February 27, 2015 15:50
Show Gist options
  • Save washingtonsoares/b0498ad14c1dd49275fe to your computer and use it in GitHub Desktop.
Save washingtonsoares/b0498ad14c1dd49275fe to your computer and use it in GitHub Desktop.
angular.module('starter.services', [])
.factory('Chats', function($http) {
// Might use a resource here that returns a JSON array
chats = [];
$http.get('http://localhost:3000/api/chats.json')
.success(
function (data) {
console.log(data); //aqui ele consegue exibir os dados
chats = data;
console.log(chats); //aqui eu passo os valores de "data" para a variavel "chats"
})
.error(
function (data) {
console.log('server side error occurred. !!!!!!! ');
});
console.log(chats); //aqui ele exibe um Array vazio
return {
all: function() {
return chats;
},
remove: function(chat) {
chats.splice(chats.indexOf(chat), 1);
},
get: function(chatId) {
for (var i = 0; i < chats.length; i++) {
if (chats[i].id === parseInt(chatId)) {
return chats[i];
}
}
return null;
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment