Forked from SubhasisDutta/gist:42b1aa75fa4d6a4316c4
Last active
August 29, 2015 14:21
-
-
Save lucasmlessa/ddecc578903fc3e0700d 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
| $(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