-
Notifications
You must be signed in to change notification settings - Fork 45
Homework #46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: homework
Are you sure you want to change the base?
Homework #46
Changes from all commits
99e3f89
67eea32
2efc149
cf47814
197337e
82daf8e
7fa5473
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,37 +1,44 @@ | ||
| 'use strict'; | ||
|
|
||
| (function() { | ||
| var _wrapper = document.querySelector('#note-content-wrapper'); | ||
| var ContentManager =(function() { | ||
|
|
||
| function start() { | ||
| window.addEventListener('note-open', function(event) { | ||
| var note = event.detail; | ||
| resetWrapper(); | ||
| drawNote(note); | ||
| }); | ||
| } | ||
| // constructor | ||
| var self = function () { | ||
| this._wrapper = null; | ||
| }; | ||
|
|
||
| // prototype | ||
| self.prototype = { | ||
|
|
||
| function resetWrapper() { | ||
| _wrapper.innerHTML = ''; | ||
| } | ||
| start() { | ||
| this._wrapper = document.querySelector('#note-content-wrapper'); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of using an anonymous function, setting the listener as |
||
| window.addEventListener('note-open', (function(event) { | ||
| var note = event.detail; | ||
| this.resetWrapper(); | ||
| this.drawNote(note); | ||
| }).bind(this)); | ||
| }, | ||
|
|
||
| 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); | ||
| } | ||
| resetWrapper() { | ||
| this._wrapper.innerHTML = ''; | ||
| }, | ||
|
|
||
| document.addEventListener('DOMContentLoaded', function(event) { | ||
| start(); | ||
| }); | ||
| })(); | ||
| 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); | ||
| } | ||
|
|
||
| }; | ||
| return self; | ||
| })(window); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,79 +1,100 @@ | ||
| 'use strict'; | ||
|
|
||
| (function() { | ||
| var ListManager = (function() { | ||
|
|
||
| // constructor | ||
| var self = function() { | ||
| this._listNoteContent = []; | ||
| this._wrapper = null; | ||
| }; | ||
|
|
||
| var _listNoteContent = []; | ||
| var _wrapper = document.querySelector('#note-list-wrapper'); | ||
| // prototype | ||
| self.prototype = { | ||
|
|
||
| function start() { | ||
| fetchList(function(data) { | ||
| updateList(data); | ||
| drawList(); | ||
| preloadFirstNote(); | ||
| }); | ||
| window.addEventListener('click', function(event) { | ||
| onNoteOpen(event); | ||
| }); | ||
| } | ||
| start() { | ||
|
|
||
| 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 })); | ||
| }; | ||
| } | ||
| this._wrapper = document.querySelector('#note-list-wrapper'); | ||
| this.fetchList() | ||
| .then(this.handler.bind(this)); | ||
|
|
||
| function preloadFirstNote() { | ||
| if (_listNoteContent.length !== 0) { | ||
| var content = _listNoteContent[0]; | ||
| window.dispatchEvent(new CustomEvent('note-open', | ||
| { detail: content })); | ||
| } | ||
| } | ||
| window.addEventListener('click', (function(event) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a convention to prefix method with |
||
| this.onNoteOpen(event); | ||
| }).bind(this)); | ||
| }, | ||
|
|
||
| function updateList(list) { | ||
| _listNoteContent = list; | ||
| } | ||
| handler(data){ | ||
| this.updateList(data); | ||
| this.drawList(); | ||
| this.preloadFirstNote(); | ||
| }, | ||
|
|
||
| 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); | ||
| } | ||
| 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 })); | ||
| } | ||
| }, | ||
|
|
||
| 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. | ||
| preloadFirstNote() { | ||
| if (this._listNoteContent.length !== 0) { | ||
| var content = this._listNoteContent[0]; | ||
| window.dispatchEvent(new CustomEvent('note-open', | ||
| { detail: content })); | ||
| } | ||
| }; | ||
| xhr.send(); | ||
| } | ||
| }, | ||
|
|
||
| updateList(list) { | ||
| this._listNoteContent = list; | ||
| }, | ||
|
|
||
| document.addEventListener('DOMContentLoaded', function(event) { | ||
| start(); | ||
| }); | ||
| 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); | ||
| }, | ||
|
|
||
| fetchList(afterFetch) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good, you have successfully "Promisified" it. |
||
| return new Promise(function(resolve, reject) { | ||
| var xhr = new XMLHttpRequest(); | ||
| var url = 'http://127.0.0.1:8000/demo-list-notes.json'; | ||
| xhr.open('GET', url, true); | ||
| xhr.responseType = 'json'; | ||
| xhr.onreadystatechange = ((function(e) { | ||
| // Watch out: we have a mysterious unknown 'this'. | ||
| // here "this" is a xmlhttprequest = xhr -> so bind xhr | ||
| if (this.readyState === 4 && this.status === 200) { | ||
| var listData = this.response; | ||
| // The flow ends here. | ||
| resolve(listData); | ||
| } else if (this.status !== 200 ){ | ||
| // Ignore error in this case. | ||
| reject("Error"); | ||
| } | ||
| }).bind(xhr)); | ||
| xhr.send(); | ||
| }); | ||
| }, | ||
|
|
||
| // test function here | ||
| // test num1 add num2 | ||
| testFunction(num1, num2) { | ||
| return num1 + num2; | ||
| } | ||
| }; | ||
| return self ; | ||
|
|
||
| })(); | ||
| })(window); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| 'use strict'; | ||
|
|
||
| document.addEventListener('DOMContentLoaded', function(event) { | ||
| var contentManager = new ContentManager(); | ||
| var listManager = new ListManager(); | ||
| contentManager.start(); | ||
| listManager.start(); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,14 @@ | ||
| describe('Test > ', function() { | ||
| describe('Test ListManager ', function() { | ||
| var listManager; | ||
| beforeEach(function() { | ||
| listManager = new ListManager() | ||
| }); | ||
|
|
||
| it('will test some pure functions', function() { | ||
| it('will test add function', function() { | ||
| // Write any pure function assertion here. | ||
| var num1 = 2; | ||
| var num2 = 10; | ||
| var sum = listManager.testFunction(num1, num2); | ||
| assert.equal(sum, 12, "Equal to 12"); | ||
| }); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It may not need to create a inner function as the constructor, while the
ContentManageris the constructor. And to overwrite the keywordselfis not so good.