-
Notifications
You must be signed in to change notification settings - Fork 45
Homework #30
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 #30
Changes from all commits
ba02d4f
0b600d0
0a54cd4
119d2e7
189c863
3e72c65
eac88c4
5105ee3
36ae82c
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,43 @@ | ||
| 'use strict'; | ||
|
|
||
| (function() { | ||
| var _wrapper = document.querySelector('#note-content-wrapper'); | ||
| (function (exports){ | ||
|
|
||
| function start() { | ||
| window.addEventListener('note-open', function(event) { | ||
| var note = event.detail; | ||
| resetWrapper(); | ||
| drawNote(note); | ||
| }); | ||
| var ContentManager = function(){ | ||
| this._wrapper = document.querySelector('#note-content-wrapper'); | ||
| } | ||
|
|
||
| function resetWrapper() { | ||
| _wrapper.innerHTML = ''; | ||
| } | ||
| ContentManager.prototype = { | ||
| start() { | ||
| window.addEventListener('note-open', (function(event) { | ||
| var note = event.detail; | ||
| this.resetWrapper(); | ||
| this.drawNote(note); | ||
| }).bind(this)); | ||
| }, | ||
| // original content.js start function | ||
| // call original contest.js resetWrapper function use "this" | ||
| // following are the same | ||
| resetWrapper(){ | ||
| _wrapper.innerHTML = ''; | ||
| }, | ||
|
|
||
| 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); | ||
| 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); | ||
| } | ||
| } | ||
|
|
||
| document.addEventListener('DOMContentLoaded', function(event) { | ||
| start(); | ||
| }); | ||
| })(); | ||
| exports.ContentManager = ContentManager; | ||
|
|
||
| })(window); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,43 +1,53 @@ | ||
| 'use strict'; | ||
|
|
||
| (function() { | ||
| //the way of how to refactor list.js is the same as content.js | ||
| //repair some error in list.js | ||
| // add promise into it | ||
|
|
||
| var _listNoteContent = []; | ||
| var _wrapper = document.querySelector('#note-list-wrapper'); | ||
| (function(exports) { | ||
|
|
||
| function start() { | ||
| fetchList(function(data) { | ||
| updateList(data); | ||
| drawList(); | ||
| preloadFirstNote(); | ||
| }); | ||
| window.addEventListener('click', function(event) { | ||
| onNoteOpen(event); | ||
| var ListManager = function(){ | ||
| this._listNoteContent = []; | ||
| this._wrapper = document.querySelector("#note-content-wrapper"); | ||
| } | ||
|
|
||
| ListManager.prototype = { | ||
| start() { | ||
| this.fetchList().then((function(data) { | ||
|
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 recommended to use Arrow function instead of calling https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions |
||
| this.updateList(data); | ||
| this.drawList(); | ||
| this.preloadFirstNote(); | ||
| }).bind(this)) | ||
| .catch((function () { | ||
|
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. Serious issue: you need to catch the error in order to log and re-throw it. The reason is logging can prevent the following chains ignore it and make troubles when you're debugging, while re-throw it to make sure the next chain will be interrupted, rather than to perform the actions which may lead to unexpected behaviors. |
||
| }).bind(this)); | ||
|
|
||
| window.addEventListener('click', (function(event) { | ||
| this.onNoteOpen(event).bind(this)); | ||
|
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 |
||
| }); | ||
| } | ||
|
|
||
| function onNoteOpen(event) { | ||
| if (event.target.classList.contains('note-title')) { | ||
| 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', | ||
| preloadFirstNote() { | ||
| if (_listNoteContent.length !== 0) { | ||
|
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. If a function body is wrapped by a conditional statement with only one branch, it's better to move the |
||
| var content = _listNoteContent[0]; | ||
| window.dispatchEvent(new CustomEvent('note-open', | ||
| { detail: content })); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function updateList(list) { | ||
| _listNoteContent = list; | ||
| updateList(list) { | ||
| this. _listNoteContent = list; | ||
| } | ||
|
|
||
| function drawList() { | ||
| drawList() { | ||
| var list = _listNoteContent; | ||
| var ul = document.createElement('ul'); | ||
| ul.id = 'note-title-list'; | ||
|
|
@@ -52,28 +62,26 @@ | |
| buff.appendChild(li); | ||
| }); | ||
| ul.appendChild(buff); | ||
| _wrapper.appendChild(ul); | ||
| this._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) { | ||
| 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. nit: You have successfully "Promisified" it, so you don't need to keep this callback-style argument. |
||
| 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); | ||
| resolve(listData) | ||
| } else if (this.status !== 200 ){ | ||
| // Ignore error in this case. | ||
| reject('ERROR'); | ||
| } | ||
| }; | ||
| xhr.send(); | ||
| }).bind(xhr); | ||
| xhr.send(); | ||
| }); | ||
| } | ||
|
|
||
| document.addEventListener('DOMContentLoaded', function(event) { | ||
| start(); | ||
| }); | ||
|
|
||
| })(); | ||
| } | ||
| exports.ListManager = ListManager; | ||
| })(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 |
|---|---|---|
|
|
@@ -2,7 +2,11 @@ describe('Test > ', function() { | |
| beforeEach(function() { | ||
| }); | ||
|
|
||
| it('will test some pure functions', function() { | ||
| it('my pure testfunctions', function() { | ||
| // Write any pure function assertion here. | ||
| var a = 1; | ||
| var b = 2; | ||
| var sum = a+b; | ||
| assert.equal(3,sum); | ||
|
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. WRONG: you need to define a pure function (method) in one manager, and then call it here. |
||
| }); | ||
| }); | ||
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.
Instead of binding a function as the listener, you should set the argument as
thisand deal with the event in thehandleEventmethod.