-
-
Save movibe/9456f85787e8502a13d0 to your computer and use it in GitHub Desktop.
This file contains 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('starter.services', []) | |
.factory('Chats', function($http) { | |
var self = this; | |
var data = { | |
chats: {} | |
}; | |
self.sync = function() { | |
$http.get('http://localhost:3000/api/chats.json') | |
.success(function(data) { | |
data.chats = data; | |
return data; | |
console.log(data); | |
}).error(function(data) { | |
console.log('server side error occurred. !!!!!!! '); | |
}); | |
}; | |
self.all = function() { | |
return data; | |
}; | |
self.remove = function(chat) { | |
data.chats.splice(chat.indexOf(chat), 1); | |
}; | |
self.get = function(chatId) { | |
for (var i = 0; i < data.chats.length; i++) { | |
if (data.chats[i].id === parseInt(chatId)) { | |
return data.chats[i]; | |
} | |
} else { | |
return null; | |
} | |
}; | |
return self; | |
}) | |
.controller('ChatCtrl', function ($scope, Chats) { | |
Chats.sync(); | |
$scope.chats = Chats.all(); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment