Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 22 additions & 16 deletions homework/content.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
'use strict';

(function() {
var _wrapper = document.querySelector('#note-content-wrapper');
(function(exports) {

var TodoContentManager = function() {
// Will be an element as the list wrapper.
this._wrapper = document.querySelector('#note-content-wrapper');
};

function start() {
window.addEventListener('note-open', function(event) {
TodoContentManager.prototype = {

start() {
window.addEventListener(('note-open', function(event) {
var note = event.detail;
resetWrapper();
drawNote(note);
});
}
}).bind(this));
},

function resetWrapper() {
_wrapper.innerHTML = '';
}
resetWrapper() {
this._wrapper.innerHTML = '';
},

function drawNote(note) {
drawNote(note) {
var title = note.title;
var h = document.createElement('h2');
h.textContent = title;
Expand All @@ -27,11 +33,11 @@
p.textContent = passage;
buff.appendChild(p);
});
_wrapper.appendChild(h);
_wrapper.appendChild(buff);
this._wrapper.appendChild(h);
this._wrapper.appendChild(buff);
}
};

document.addEventListener('DOMContentLoaded', function(event) {
start();
});
})();

exports.TodoContentManager = TodoContentManager;
})(window);
1 change: 1 addition & 0 deletions homework/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<head>
<meta charset="UTF-8">
<title> Homework - Note List </title>
<script defer src="main.js"></script>
<script defer src="list.js"></script>
<script defer src="content.js"></script>
<link rel="stylesheet" type="text/css" href="main.css" />
Expand Down
100 changes: 52 additions & 48 deletions homework/list.js
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) {
Copy link
Contributor

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 on for the corresponding function, so it should be renamed as 'onClick'.

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();
Expand All @@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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);
8 changes: 8 additions & 0 deletions homework/main.js
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();
});
9 changes: 9 additions & 0 deletions homework/test/test-list.js
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){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. There is no Manager constructor in this file and the included files
  2. Put it in any manager you have defined
  3. Are you sure this works well?

return a+b;
}
});