Skip to content

Instantly share code, notes, and snippets.

@americoneto1
Created March 14, 2014 01:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save americoneto1/9540837 to your computer and use it in GitHub Desktop.
Save americoneto1/9540837 to your computer and use it in GitHub Desktop.
Modulo TO DO com LocalStorage
var APPMAIL = (function() {
var box = {};
box.txtEmail = document.querySelector('#email');
box.btnAdicionar = document.querySelector('.pure-button');
box.lstEmails = document.querySelector('ul');
box.validateData = function() {
box.txtEmail.focus();
var pattern = /^\w{2,}\@\w+\.[a-zA-Z]{2,4}(\.[a-zA-Z]{2})?$/;
if(pattern.test(box.txtEmail.value)) {
box.txtEmail.className = "";
return true;
} else {
box.txtEmail.className = "error";
return false;
}
};
box.saveData = function(email) {
if(localStorage && localStorage.getItem("emailList")) {
var lista = JSON.parse(localStorage.getItem("emailList"));
} else {
var lista = [];
}
lista.push({account: email});
localStorage.setItem("emailList", JSON.stringify(lista));
};
box.updateScreen = function() {
if(localStorage && localStorage.getItem("emailList")) {
var lista = JSON.parse(localStorage.getItem("emailList"));
box.lstEmails.innerHTML = "";
for(var i = 0; i < lista.length; i++) {
var li = document.createElement('li');
li.innerHTML = lista[i].account;
box.lstEmails.appendChild(li);
}
}
};
box.initialize = function() {
box.btnAdicionar.addEventListener("click", function(event) {
event.preventDefault();
if(box.validateData()) {
box.saveData(box.txtEmail.value);
box.txtEmail.value = "";
box.updateScreen();
}
});
box.updateScreen();
}();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment