diff --git a/homework/content.js b/homework/content.js index 710be28..e508a30 100644 --- a/homework/content.js +++ b/homework/content.js @@ -1,37 +1,44 @@ 'use strict'; -(function() { - var _wrapper = document.querySelector('#note-content-wrapper'); +(function(exports) { + var NoteContentManager = function() { + this._wrapper = document.querySelector('#note-content-wrapper'); + }; - function start() { - window.addEventListener('note-open', function(event) { - var note = event.detail; - resetWrapper(); - drawNote(note); - }); - } + NoteContentManager.prototype = { + start() { + window.addEventListener('note-open', function(event) { + var note = event.detail; + this.resetWrapper() + .then((function(){ + this.drawNote(note); + }).bind(this)); + }.bind(this)); + }, + + resetWrapper() { + this._wrapper.innerHTML = ''; + return Promise.resolve(); + }, - function resetWrapper() { - _wrapper.innerHTML = ''; - } + drawNote(note) { + var title = note.title; + var h = document.createElement('h2'); + h.textContent = title; + var passages = note.passages; + var buff = document.createDocumentFragment(); + passages.forEach(function(passage) { + var p = document.createElement('p'); + p.classList.add('note-passage'); + p.textContent = passage; + buff.appendChild(p); + }); + this._wrapper.appendChild(h); + this._wrapper.appendChild(buff); + } + + }; + + exports.NoteContentManager = NoteContentManager; - function drawNote(note) { - var title = note.title; - var h = document.createElement('h2'); - h.textContent = title; - var passages = note.passages; - var buff = document.createDocumentFragment(); - passages.forEach(function(passage) { - var p = document.createElement('p'); - p.classList.add('note-passage'); - p.textContent = passage; - buff.appendChild(p); - }); - _wrapper.appendChild(h); - _wrapper.appendChild(buff); - } - - document.addEventListener('DOMContentLoaded', function(event) { - start(); - }); -})(); +})(window); diff --git a/homework/index.html b/homework/index.html index e387963..9f38fbd 100644 --- a/homework/index.html +++ b/homework/index.html @@ -3,7 +3,8 @@ Homework - Note List - + + diff --git a/homework/list.js b/homework/list.js index 21eddbc..9277a93 100644 --- a/homework/list.js +++ b/homework/list.js @@ -1,79 +1,92 @@ 'use strict'; -(function() { +(function(exports) { + var TodoListManager = function() { + this._listNoteContent = []; + this._wrapper = document.querySelector('#note-list-wrapper'); + }; - var _listNoteContent = []; - var _wrapper = document.querySelector('#note-list-wrapper'); + TodoListManager.prototype = { + start() { + this.fetchList(function(data) { + this.updateList(data) + .then((function(){ + this.drawList(); + }).bind(this)).then((function(){ + this.preloadFirstNote(); + }).bind(this)).catch((function(error){ + }).bind(this)); + }.bind(this)); + + + window.addEventListener('click', function(event) { + this.onNoteOpen(event); + }.bind(this)); + }, + + onNoteOpen(event) { + if (event.target.classList.contains('note-title')) { + var id = event.target.dataset.noteId; + var content = this._listNoteContent[id]; + window.dispatchEvent(new CustomEvent('note-open',{ detail: content })); + }; + }, + + preloadFirstNote() { + if (this._listNoteContent.length !== 0) { + var content = this._listNoteContent[0]; + window.dispatchEvent(new CustomEvent('note-open',{ detail: content })); + } + return Promise.resolve(); + }, + + updateList(list) { + this._listNoteContent = list; + return Promise.resolve(); + }, + + drawList() { + var list = this._listNoteContent; + var ul = document.createElement('ul'); + ul.id = 'note-title-list'; + var buff = document.createDocumentFragment(); + list.forEach(function(note, i) { + var li = document.createElement('li'); + li.dataset.noteId = i; + li.classList.add('note-title'); + li.textContent = note.title; + // Note: buff is captured, so we now have a + // little closure naturally. + buff.appendChild(li); + }); + ul.appendChild(buff); + this._wrapper.appendChild(ul); + return Promise.resolve(); + }, + + fetchList(afterFetch) { + return new Promise((function(resolve, reject) { + var xhr = new XMLHttpRequest(); + xhr.open('GET', 'http://127.0.0.1/hw1/demo-list-notes.json', true); + xhr.responseType = 'json'; + xhr.onreadystatechange = function(e) { + // Watch out: we have a mysterious unknown 'this'. + if (this.readyState === 4 && this.status === 200) { + var listData = this.response; + // The flow ends here. + afterFetch(listData); + } else if (this.status !== 200 ){ + // Ignore error in this case. + } + }.bind(xhr); + xhr.send(); + }).bind(this)); + } + + }; - function start() { - fetchList(function(data) { - updateList(data); - drawList(); - preloadFirstNote(); - }); - window.addEventListener('click', function(event) { - onNoteOpen(event); - }); - } + exports.TodoListManager = TodoListManager; - function onNoteOpen(event) { - if (event.target.classList.contains('note-title')) { - var id = event.target.dataset.noteId; - var content = _listNoteContent[id]; - window.dispatchEvent(new CustomEvent('note-open', - { detail: content })); - }; - } + - function preloadFirstNote() { - if (_listNoteContent.length !== 0) { - var content = _listNoteContent[0]; - window.dispatchEvent(new CustomEvent('note-open', - { detail: content })); - } - } - - function updateList(list) { - _listNoteContent = list; - } - - function drawList() { - var list = _listNoteContent; - var ul = document.createElement('ul'); - ul.id = 'note-title-list'; - var buff = document.createDocumentFragment(); - list.forEach(function(note, i) { - var li = document.createElement('li'); - li.dataset.noteId = i; - li.classList.add('note-title'); - li.textContent = note.title; - // Note: buff is captured, so we now have a - // little closure naturally. - buff.appendChild(li); - }); - ul.appendChild(buff); - _wrapper.appendChild(ul); - } - - function fetchList(afterFetch) { - var xhr = new XMLHttpRequest(); - xhr.open('GET', 'http://127.0.0.1:8000/demo-list-notes.json', true); - xhr.responseType = 'json'; - xhr.onreadystatechange = function(e) { - // Watch out: we have a mysterious unknown 'this'. - if (this.readyState === 4 && this.status === 200) { - var listData = this.response; - // The flow ends here. - afterFetch(listData); - } else if (this.status !== 200 ){ - // Ignore error in this case. - } - }; - xhr.send(); - } - - document.addEventListener('DOMContentLoaded', function(event) { - start(); - }); - -})(); +})(window); diff --git a/homework/main.js b/homework/main.js new file mode 100644 index 0000000..a4c6da3 --- /dev/null +++ b/homework/main.js @@ -0,0 +1,6 @@ +document.addEventListener('DOMContentLoaded', function(event) { + var todoListManager = new TodoListManager(); + var noteContentManager = new NoteContentManager(); + todoListManager.start(); + noteContentManager.start(); +}); \ No newline at end of file diff --git a/homework/test/test-list.js b/homework/test/test-list.js index 441fc45..d49bb99 100644 --- a/homework/test/test-list.js +++ b/homework/test/test-list.js @@ -1,8 +1,24 @@ +var MathFunc=function(){}; + +MathFunc.prototype = { + + addNumber: function(a,b){ + return (a+b); + } +}; + + +var assert= require("assert"); describe('Test > ', function() { beforeEach(function() { + mathFunc = new MathFunc(); }); - it('will test some pure functions', function() { + it('will add two numbers', function() { + assert.equal(16,mathFunc.addNumber(15, 1)); + // Write any pure function assertion here. }); }); + +