From 04c11feb950738fdb30e94a6ed16c007f1f022b7 Mon Sep 17 00:00:00 2001 From: DannyJJJJ Date: Fri, 30 Oct 2015 11:14:33 +0800 Subject: [PATCH 01/10] Update content.js First change according to teacher's example 1 to exampe 5 --- homework/content.js | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/homework/content.js b/homework/content.js index 710be28..ab60f32 100644 --- a/homework/content.js +++ b/homework/content.js @@ -1,21 +1,29 @@ 'use strict'; -(function() { - var _wrapper = document.querySelector('#note-content-wrapper'); +(function(exports) { + + var TodoContentManager = function() { + // Local data storage; should sync up with the server. + this._listTodoItem = []; + // Will be an element as the list wrapper. + this._wrapper = document.querySelector('#note-content-wrapper'); +}; - function start() { +TodoContentManager.prototype = { + + start() { window.addEventListener('note-open', function(event) { var note = event.detail; resetWrapper(); drawNote(note); - }); - } + }).bind(this); + }, - function resetWrapper() { - _wrapper.innerHTML = ''; - } + resetWrapper() { + this._wrapper.innerHTML = ''; + }, - function drawNote(note) { + drawNote(note) { var title = note.title; var h = document.createElement('h2'); h.textContent = title; @@ -27,11 +35,11 @@ p.textContent = passage; buff.appendChild(p); }); - _wrapper.appendChild(h); - _wrapper.appendChild(buff); + this._wrapper.appendChild(h); + this._wrapper.appendChild(buff); } +}; - document.addEventListener('DOMContentLoaded', function(event) { - start(); - }); -})(); + + exports.TodoContentManager = TodoListManager; +})(window); From df136ea70e8a53733c1610aaecf585b32fdad437 Mon Sep 17 00:00:00 2001 From: DannyJJJJ Date: Fri, 30 Oct 2015 11:16:02 +0800 Subject: [PATCH 02/10] Update content.js some small error fix --- homework/content.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/homework/content.js b/homework/content.js index ab60f32..56d93f5 100644 --- a/homework/content.js +++ b/homework/content.js @@ -3,8 +3,6 @@ (function(exports) { var TodoContentManager = function() { - // Local data storage; should sync up with the server. - this._listTodoItem = []; // Will be an element as the list wrapper. this._wrapper = document.querySelector('#note-content-wrapper'); }; @@ -41,5 +39,5 @@ TodoContentManager.prototype = { }; - exports.TodoContentManager = TodoListManager; + exports.TodoContentManager = TodoContentManager; })(window); From e85c049061102069fb1939a76115c9340337748e Mon Sep 17 00:00:00 2001 From: DannyJJJJ Date: Fri, 30 Oct 2015 11:21:08 +0800 Subject: [PATCH 03/10] Update list.js first function to obiect change --- homework/list.js | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/homework/list.js b/homework/list.js index 21eddbc..6ef696f 100644 --- a/homework/list.js +++ b/homework/list.js @@ -1,11 +1,15 @@ 'use strict'; -(function() { - - var _listNoteContent = []; - var _wrapper = document.querySelector('#note-list-wrapper'); - - function start() { +(function(exports) { + + var TodoListManager = function() { + this._listNoteContent = []; + this._wrapper = document.querySelector('#note-list-wrapper'); + }; + +TodoListManager.prototype = { + + start() { fetchList(function(data) { updateList(data); drawList(); @@ -14,9 +18,9 @@ window.addEventListener('click', function(event) { onNoteOpen(event); }); - } + }, - function onNoteOpen(event) { + onNoteOpen(event) { if (event.target.classList.contains('note-title')) { var id = event.target.dataset.noteId; var content = _listNoteContent[id]; @@ -25,19 +29,19 @@ }; } - function preloadFirstNote() { + preloadFirstNote() { if (_listNoteContent.length !== 0) { var content = _listNoteContent[0]; window.dispatchEvent(new CustomEvent('note-open', { detail: content })); } - } + }, - function updateList(list) { + updateList(list) { _listNoteContent = list; - } + }, - function drawList() { + drawList() { var list = _listNoteContent; var ul = document.createElement('ul'); ul.id = 'note-title-list'; @@ -53,9 +57,9 @@ }); ul.appendChild(buff); _wrapper.appendChild(ul); - } + }, - function fetchList(afterFetch) { + fetchList(afterFetch) { var xhr = new XMLHttpRequest(); xhr.open('GET', 'http://127.0.0.1:8000/demo-list-notes.json', true); xhr.responseType = 'json'; @@ -71,9 +75,7 @@ }; xhr.send(); } +} - document.addEventListener('DOMContentLoaded', function(event) { - start(); - }); - -})(); +exports.TodoListManager = TodoListManager; +})(window); From ed13ceacca9688ed00f3cbc9bf8680ad13aab4a9 Mon Sep 17 00:00:00 2001 From: DannyJJJJ Date: Fri, 30 Oct 2015 11:25:31 +0800 Subject: [PATCH 04/10] Update content.js --- homework/content.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homework/content.js b/homework/content.js index 56d93f5..cecd96c 100644 --- a/homework/content.js +++ b/homework/content.js @@ -14,7 +14,7 @@ TodoContentManager.prototype = { var note = event.detail; resetWrapper(); drawNote(note); - }).bind(this); + }).bind(this)); }, resetWrapper() { From 1ead9d40999ac6a37d93e0bd56362b3f6228d556 Mon Sep 17 00:00:00 2001 From: DannyJJJJ Date: Fri, 30 Oct 2015 11:37:41 +0800 Subject: [PATCH 05/10] Update content.js --- homework/content.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homework/content.js b/homework/content.js index cecd96c..cf51b6d 100644 --- a/homework/content.js +++ b/homework/content.js @@ -10,7 +10,7 @@ TodoContentManager.prototype = { start() { - window.addEventListener('note-open', function(event) { + window.addEventListener(('note-open', function(event) { var note = event.detail; resetWrapper(); drawNote(note); From 0e86b8377881819c530b7041dfeb79c45e535200 Mon Sep 17 00:00:00 2001 From: DannyJJJJ Date: Fri, 30 Oct 2015 11:46:56 +0800 Subject: [PATCH 06/10] Update list.js --- homework/list.js | 58 +++++++++++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 28 deletions(-) diff --git a/homework/list.js b/homework/list.js index 6ef696f..a23d4dd 100644 --- a/homework/list.js +++ b/homework/list.js @@ -10,39 +10,39 @@ TodoListManager.prototype = { start() { - fetchList(function(data) { - updateList(data); - drawList(); - preloadFirstNote(); - }); - window.addEventListener('click', function(event) { - onNoteOpen(event); - }); + this.fetchList((function(data) { + this.updateList(data); + this.drawList(); + this.preloadFirstNote(); + }).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 = _listNoteContent[id]; + var content = this._listNoteContent[id]; window.dispatchEvent(new CustomEvent('note-open', { detail: content })); }; } preloadFirstNote() { - if (_listNoteContent.length !== 0) { - var content = _listNoteContent[0]; + if (this._listNoteContent.length !== 0) { + var content = this_listNoteContent[0]; window.dispatchEvent(new CustomEvent('note-open', { detail: content })); } }, updateList(list) { - _listNoteContent = list; + this._listNoteContent = list; }, drawList() { - var list = _listNoteContent; + var list = this._listNoteContent; var ul = document.createElement('ul'); ul.id = 'note-title-list'; var buff = document.createDocumentFragment(); @@ -56,24 +56,26 @@ TodoListManager.prototype = { buff.appendChild(li); }); ul.appendChild(buff); - _wrapper.appendChild(ul); + this._wrapper.appendChild(ul); }, 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(); + return new Promise((function(resolve, reject) { + 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(); + }).bind(this)); } } From 0d327d29ee6e61e9109faf6e03e00c60090185d9 Mon Sep 17 00:00:00 2001 From: DannyJJJJ Date: Fri, 30 Oct 2015 11:49:34 +0800 Subject: [PATCH 07/10] Create main.js --- homework/main.js | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 homework/main.js diff --git a/homework/main.js b/homework/main.js new file mode 100644 index 0000000..ba6648d --- /dev/null +++ b/homework/main.js @@ -0,0 +1,8 @@ +'use strict'; + +document.addEventListener('DOMContentLoaded',function(event){ + var ToDoContentManager = new ToDoContentManager(); + var ToDoListManager = new ToDoListManager(); + ToDoContentManager.start(); + ToDoListManager.start(); +}); From b57dbd1db8e4000828730ca3d3c31adca55c419b Mon Sep 17 00:00:00 2001 From: DannyJJJJ Date: Fri, 30 Oct 2015 11:49:59 +0800 Subject: [PATCH 08/10] Update index.html --- homework/index.html | 1 + 1 file changed, 1 insertion(+) diff --git a/homework/index.html b/homework/index.html index e387963..ad28075 100644 --- a/homework/index.html +++ b/homework/index.html @@ -3,6 +3,7 @@ Homework - Note List + From 55c6a91bba18ad5dadc7da930ec05b3cb8e3d5bf Mon Sep 17 00:00:00 2001 From: DannyJJJJ Date: Fri, 30 Oct 2015 11:54:21 +0800 Subject: [PATCH 09/10] Update test-list.js --- homework/test/test-list.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/homework/test/test-list.js b/homework/test/test-list.js index 441fc45..03bbb33 100644 --- a/homework/test/test-list.js +++ b/homework/test/test-list.js @@ -1,8 +1,13 @@ describe('Test > ', function() { + var subject; beforeEach(function() { + subject = new ToDoListManager(); }); it('will test some pure functions', function() { // Write any pure function assertion here. + var test = subject; + var sum = test.puerFunction(1,4); + assert.equal(sum,5,'It is equal to 5!'); }); }); From d203235a577ef7a2a661b9948c6498699b89146c Mon Sep 17 00:00:00 2001 From: DannyJJJJ Date: Fri, 30 Oct 2015 12:09:31 +0800 Subject: [PATCH 10/10] Update test-list.js --- homework/test/test-list.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/homework/test/test-list.js b/homework/test/test-list.js index 03bbb33..a1a53b3 100644 --- a/homework/test/test-list.js +++ b/homework/test/test-list.js @@ -10,4 +10,8 @@ describe('Test > ', function() { var sum = test.puerFunction(1,4); assert.equal(sum,5,'It is equal to 5!'); }); + + Manager.prototype.puerFunction(a,b){ + return a+b; + } });