-
Notifications
You must be signed in to change notification settings - Fork 45
101502012 #45
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?
101502012 #45
Changes from all commits
04c11fe
df136ea
e85c049
ed13cea
1ead9d4
0e86b83
0d327d2
b57dbd1
55c6a91
d203235
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,44 +1,48 @@ | ||
| 'use strict'; | ||
|
|
||
| (function() { | ||
| (function(exports) { | ||
|
|
||
| var TodoListManager = function() { | ||
| this._listNoteContent = []; | ||
| this._wrapper = document.querySelector('#note-list-wrapper'); | ||
| }; | ||
|
|
||
| TodoListManager.prototype = { | ||
|
|
||
| start() { | ||
| this.fetchList((function(data) { | ||
| this.updateList(data); | ||
| this.drawList(); | ||
| this.preloadFirstNote(); | ||
| }).bind(this)); | ||
| window.addEventListener(('click', function(event) { | ||
| this.onNoteOpen(event); | ||
| }).bind(this)); | ||
| }, | ||
|
|
||
| var _listNoteContent = []; | ||
| var _wrapper = document.querySelector('#note-list-wrapper'); | ||
|
|
||
| function start() { | ||
| fetchList(function(data) { | ||
| updateList(data); | ||
| drawList(); | ||
| preloadFirstNote(); | ||
| }); | ||
| 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]; | ||
| 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) { | ||
| 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 +56,28 @@ | |
| 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 ){ | ||
| // Ignore error in this case. | ||
| } | ||
| }; | ||
| xhr.send(); | ||
| 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. It's wrong here: if you are using a Promise, you definitely needn't a callback-style method. Instead of, you return a Promise. Please check Promise example like this: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise#Examples I suggest that you better to check and correct it, or you're just showing that you haven't understood Promise well. |
||
| 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)); | ||
| } | ||
| } | ||
|
|
||
| document.addEventListener('DOMContentLoaded', function(event) { | ||
| start(); | ||
| }); | ||
|
|
||
| })(); | ||
| exports.TodoListManager = TodoListManager; | ||
| })(window); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| 'use strict'; | ||
|
|
||
| document.addEventListener('DOMContentLoaded',function(event){ | ||
| var ToDoContentManager = new ToDoContentManager(); | ||
| var ToDoListManager = new ToDoListManager(); | ||
| ToDoContentManager.start(); | ||
| ToDoListManager.start(); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,17 @@ | ||
| 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!'); | ||
| }); | ||
|
|
||
| Manager.prototype.puerFunction(a,b){ | ||
|
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.
|
||
| return a+b; | ||
| } | ||
| }); | ||
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 a convention to prefix method with
onfor the corresponding function, so it should be renamed as 'onClick'.