Created
March 14, 2014 01:57
-
-
Save americoneto1/9540837 to your computer and use it in GitHub Desktop.
Modulo TO DO com LocalStorage
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
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