Skip to content

Instantly share code, notes, and snippets.

@movibe
Forked from washingtonsoares/services.js
Last active August 29, 2015 14:16
Show Gist options
  • Save movibe/9456f85787e8502a13d0 to your computer and use it in GitHub Desktop.
Save movibe/9456f85787e8502a13d0 to your computer and use it in GitHub Desktop.
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