-
Notifications
You must be signed in to change notification settings - Fork 45
102502010 #28
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?
102502010 #28
Changes from all commits
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 |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # The slides | ||
|
|
||
| [Follow this link](https://docs.google.com/presentation/d/1ofgD63SENpiihJ_MIYyZVW2TM11Mo-qwcstDbZytqIo/edit?usp=sharing) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,44 +1,35 @@ | ||
| 'use strict'; | ||
|
|
||
| (function() { | ||
| (function(exports) { | ||
|
|
||
| var _listNoteContent = []; | ||
| var _wrapper = document.querySelector('#note-list-wrapper'); | ||
| var NoteListManager = function() { | ||
| this._listNoteContent = []; | ||
| this._wrapper = null; | ||
| }; | ||
|
|
||
| function start() { | ||
| fetchList(function(data) { | ||
| updateList(data); | ||
| drawList(); | ||
| preloadFirstNote(); | ||
| }); | ||
| window.addEventListener('click', function(event) { | ||
| onNoteOpen(event); | ||
| }); | ||
| } | ||
| NoteListManager.prototype = { | ||
|
|
||
| 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 })); | ||
| }; | ||
| } | ||
| onNoteOpen(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 |
||
| var id = event.target.dataset.noteId; | ||
| var content = this._listNoteContent[id]; | ||
| window.dispatchEvent(new CustomEvent('note-open', | ||
| { detail: content })); | ||
| }, | ||
|
|
||
| function preloadFirstNote() { | ||
| if (_listNoteContent.length !== 0) { | ||
| var content = _listNoteContent[0]; | ||
| preloadFirstNote() { | ||
| if (this._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 only with one branch, it's better to put the |
||
| var content = this._listNoteContent[0]; | ||
| window.dispatchEvent(new CustomEvent('note-open', | ||
| { detail: content })); | ||
| } | ||
| } | ||
| }, | ||
|
|
||
| function updateList(list) { | ||
| _listNoteContent = list; | ||
| } | ||
| updateList(list) { | ||
| this._listNoteContent = list; | ||
| }, | ||
|
|
||
| function drawList() { | ||
| var list = _listNoteContent; | ||
| drawList() { | ||
| var list = this._listNoteContent; | ||
| var ul = document.createElement('ul'); | ||
| ul.id = 'note-title-list'; | ||
| var buff = document.createDocumentFragment(); | ||
|
|
@@ -52,28 +43,48 @@ | |
| 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) { | ||
| // 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 ){ | ||
| fetchList() { | ||
| return new Promise((function(resolve,reject) { | ||
|
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. |
||
| var xhr = new XMLHttpRequest(); | ||
| xhr.open('GET', '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. | ||
| resolve(listData); | ||
| } else if (this.status !== 200 ){ | ||
| // Ignore error in this case. | ||
| } | ||
| }; | ||
| xhr.send(); | ||
| } | ||
| } | ||
| }; | ||
| xhr.send(); | ||
| }).bind(this)); | ||
| }, | ||
|
|
||
| document.addEventListener('DOMContentLoaded', function(event) { | ||
| start(); | ||
| }); | ||
| handleEvent(event) { | ||
| switch(event.type) { | ||
| case 'click': | ||
| if(event.target.classList.contains('note-title')) { | ||
| this.onNoteOpen(event); | ||
| } | ||
| break; | ||
| } | ||
| }, | ||
|
|
||
| start() { | ||
| window.addEventListener('click',this); | ||
| this._wrapper = document.querySelector('#note-list-wrapper'); | ||
| 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. In fact, I would recommend to use Arrow function instead of the
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. Another point is in this way you didn't practice how to chain a Promise. So although it's unnecessary, please try to make a chain with several |
||
| this.updateList(data); | ||
| this.drawList(); | ||
| this.preloadFirstNote(); | ||
| }).bind(this)); | ||
| } | ||
| }; | ||
|
|
||
| })(); | ||
| exports.NoteListManager = NoteListManager; | ||
| })(window); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| 'use strict'; | ||
|
|
||
| // Kick off | ||
| document.addEventListener('DOMContentLoaded', function(event) { | ||
| var todoListManager = new NoteListManager(); | ||
| var todocontentManager = new NotecontentManager(); | ||
| todoListManager.start(); | ||
| todocontentManager.start(); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,14 @@ | ||
| describe('Test > ', function() { | ||
| beforeEach(function() { | ||
| }); | ||
| describe('Test NoteListManager', function() { | ||
| var subject; | ||
|
|
||
| beforeEach(function() { | ||
| subject = new NoteListManager(); | ||
| }); | ||
|
|
||
| it('will test some pure functions', function() { | ||
| it('It will test drawList functions', function() { | ||
| // Write any pure function assertion here. | ||
| subject._wrapper = document.createElement('div'); | ||
| subject.drawList([]); | ||
| assert.isNull(subject._wrapper.querySelector('li')); | ||
|
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 tested a real method instead of a mocked one. |
||
| }); | ||
| }); | ||
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.
Good. It's always better to bind the
thisas event listener rather than an anonymous function.