Created
July 28, 2015 01:23
-
-
Save jouderianjr/08a87d2d6d12f8d93f0e to your computer and use it in GitHub Desktop.
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('services.message', ['ionic']) | |
.service('Message', function($rootScope, $log, $window) { | |
service = {}; | |
if( $window.localStorage['messages'] != null || $window.localStorage['messages'] != undefined){ | |
var messages = JSON.parse($window.localStorage['messages']); | |
}else{ | |
var messages = []; | |
} | |
service.getMessages = function(){ | |
return messages; | |
} | |
service.addMessage = function(message){ | |
messages.push(message); | |
$window.localStorage['messages'] = JSON.stringify(messages); | |
updateMessages('adicionada'); | |
} | |
service.editMessage = function(message, messageIndex){ | |
messages[messageIndex] = message; | |
$window.localStorage['messages'] = JSON.stringify(messages); | |
updateMessages('editada'); | |
} | |
service.deleteMessage = function(messageIndex){ | |
messages.splice(messageIndex, 1); | |
$window.localStorage['messages'] = JSON.stringify(messages); | |
updateMessages('apagada'); | |
} | |
function updateMessages(action) { | |
$rootScope.$broadcast('messagesUpdated',{action: action}); // broadcasts the update to listeners | |
} | |
service.messages = function() { | |
return messages; | |
} | |
return service; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment