Skip to content

Instantly share code, notes, and snippets.

@lucasmlessa
Forked from SubhasisDutta/gist:42b1aa75fa4d6a4316c4
Last active August 29, 2015 14:21
Show Gist options
  • Select an option

  • Save lucasmlessa/ddecc578903fc3e0700d to your computer and use it in GitHub Desktop.

Select an option

Save lucasmlessa/ddecc578903fc3e0700d to your computer and use it in GitHub Desktop.
$(function(){
var model = {
init: function() {
if (!localStorage.notes) {
localStorage.notes = JSON.stringify([]);
}
},
add: function(obj) {
var data = JSON.parse(localStorage.notes);
data.push(obj);
localStorage.notes = JSON.stringify(data);
},
getAllNotes: function() {
return JSON.parse(localStorage.notes);
}
};
var octopus = {
addNewNote: function(noteStr) {
model.add({
content: noteStr
});
view.render();
},
getNotes: function() {
return model.getAllNotes();
},
init: function() {
model.init();
view.init();
}
};
var view = {
init: function() {
this.noteList = $('#notes');
var newNoteForm = $('#new-note-form');
var newNoteContent = $('#new-note-content');
newNoteForm.submit(function(e){
octopus.addNewNote(newNoteContent.val());
newNoteContent.val('');
e.preventDefault();
});
view.render();
},
render: function(){
var htmlStr = '';
octopus.getNotes().forEach(function(note){
htmlStr += '<li class="note">'+
note.content +
'</li>';
});
this.noteList.html( htmlStr );
}
};
octopus.init();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment