-
Notifications
You must be signed in to change notification settings - Fork 45
102502038 #53
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?
102502038 #53
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 |
|---|---|---|
| @@ -1,37 +1,41 @@ | ||
| 'use strict'; | ||
|
|
||
| (function() { | ||
| var _wrapper = document.querySelector('#note-content-wrapper'); | ||
| function ToDoItemReader() { | ||
| this._wrapper = document.querySelector('#note-content-wrapper'); | ||
| } | ||
| ToDoItemReader.prototype = { | ||
| start: function() { | ||
| window.addEventListener('note-open', this); | ||
| }, | ||
|
|
||
| function start() { | ||
| window.addEventListener('note-open', function(event) { | ||
| var note = event.detail; | ||
| resetWrapper(); | ||
| drawNote(note); | ||
| }); | ||
| } | ||
| resetWrapper: function() { | ||
| this._wrapper.innerHTML = ''; | ||
| }, | ||
|
|
||
| function resetWrapper() { | ||
| _wrapper.innerHTML = ''; | ||
| } | ||
| drawNote: function(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); | ||
| }.bind(this)); | ||
| this._wrapper.appendChild(h); | ||
| this._wrapper.appendChild(buff); | ||
| }, | ||
|
|
||
| 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); | ||
| } | ||
| handleEvent: function(event) { | ||
| var note = event.detail; | ||
| this.resetWrapper(); | ||
| this.drawNote(note); | ||
| } | ||
| } | ||
| document.addEventListener('DOMContentLoaded', function(event) { | ||
| var toDoItemReader = new ToDoItemReader(); | ||
| toDoItemReader.start(); | ||
| }); | ||
|
|
||
| document.addEventListener('DOMContentLoaded', function(event) { | ||
| start(); | ||
| }); | ||
| })(); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,79 +1,83 @@ | ||
| 'use strict'; | ||
| function ToDoList(){ | ||
| this._listNoteContent = []; | ||
| this._wrapper = document.querySelector('#note-list-wrapper'); | ||
| } | ||
| ToDoList.prototype = { | ||
| start: function(){ | ||
| var promise = this.fetchList(); | ||
| promise.then( | ||
| (data) => { | ||
| this.updateList(data); | ||
| this.drawList(); | ||
| if (this._listNoteContent.length !== 0){ | ||
| this.preloadFirstNote(); | ||
| } | ||
| }); | ||
| promise.catch( | ||
| (reason) => { | ||
| throw reason; | ||
| } | ||
| ); | ||
|
Author
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. Not sure if this is what Promise code should be like.
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 unfortunately wrong. You just got trapped by a common mistake. In fact, you should do this if you don't want to use the plain chaining style: Please note it's because every time the It will NOT guarantee |
||
| window.addEventListener('click',this); | ||
| }, | ||
|
|
||
| preloadFirstNote: function() { | ||
| var content = this._listNoteContent[0]; | ||
| window.dispatchEvent(new CustomEvent('note-open', | ||
| { detail: content })); | ||
| }, | ||
|
|
||
| (function() { | ||
| updateList: function(list) { | ||
| this._listNoteContent = list; | ||
| }, | ||
|
|
||
| var _listNoteContent = []; | ||
| var _wrapper = document.querySelector('#note-list-wrapper'); | ||
| drawList: function() { | ||
| 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); | ||
| }, | ||
|
|
||
| function start() { | ||
| fetchList(function(data) { | ||
| updateList(data); | ||
| drawList(); | ||
| preloadFirstNote(); | ||
| }); | ||
| window.addEventListener('click', function(event) { | ||
| onNoteOpen(event); | ||
| }); | ||
| } | ||
| fetchList: function() { | ||
| return new Promise(function(resolve, reject){ | ||
| 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) { | ||
| // The flow ends here. | ||
| resolve(this.response); | ||
| } else if (this.status !== 200 ){ | ||
| // Ignore error in this case. | ||
| reject('Status Error: '+this.status); | ||
| } | ||
| }; | ||
| xhr.send(); | ||
| }); | ||
| }, | ||
|
|
||
| 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 })); | ||
| handleEvent: function(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 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(); | ||
| }); | ||
|
|
||
| })(); | ||
| }; | ||
| document.addEventListener('DOMContentLoaded', function(event) { | ||
| var toDoList = new ToDoList(); | ||
| toDoList.start(); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,36 @@ | ||
| 'use strict'; | ||
| describe('Test > ', function() { | ||
| beforeEach(function() { | ||
| var toDoList, toDoItemReader; | ||
| beforeEach(function() { | ||
| toDoList = new ToDoList(); | ||
| toDoItemReader = new ToDoItemReader(); | ||
| toDoList._wrapper = document.createElement('div'); | ||
| toDoItemReader._wrapper = document.createElement('div'); | ||
| }); | ||
|
|
||
| it('will test some pure functions', function() { | ||
| // Write any pure function assertion here. | ||
| }); | ||
| toDoItemReader.start(); | ||
| toDoList._listNoteContent[0] = { | ||
| title: "This is an pen", | ||
| passages: [ | ||
| "This is a pencil", | ||
| "That is a book" | ||
| ] | ||
| }; | ||
| var onNoteOpenEvent = { | ||
| target: { | ||
| dataset: { | ||
| noteId: 0, | ||
| }, | ||
| classList: { | ||
| contains: function(input){ | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
Author
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. Tried to create a real click event but fail.
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. Why did you think you need to create a real click event? I just mentioned that the name is wrong, because in the code it listen to
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. And if you need to test a event in unit tests, you just create a plain object as: And then feed it to the |
||
| toDoItemReader._wrapper.innerHTML = ""; | ||
| toDoList.handleEvent(onNoteOpenEvent); | ||
| assert.notEqual(toDoItemReader._wrapper.innerHTML, ""); | ||
| }.bind(this)); | ||
| }); | ||
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's better to put all startup code like this in one method, rather than two native handlers like this.