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
69 changes: 37 additions & 32 deletions homework/content.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,42 @@
'use strict';

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

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

function resetWrapper() {
_wrapper.innerHTML = '';
}
start : function () {
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of using an anonymous function, set the listener as this and handle events with handleEvent method is better.

window.addEventListener('note-open', (function(event) {
var note = event.detail;
this.resetWrapper();
this.drawNote(note);
}).bind(this));
},
resetWrapper : function () {
this._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);
}

document.addEventListener('DOMContentLoaded', function(event) {
start();
});
})();
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);
});
this._wrapper.appendChild(h);
this._wrapper.appendChild(buff);
},
addition: function (a,b) {
return a + b;
}
};
var content = new Content();
document.addEventListener('DOMContentLoaded', function(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 better to put all startup code like this in one method, rather than two native handlers like this.

content.start();
});
146 changes: 76 additions & 70 deletions homework/list.js
Original file line number Diff line number Diff line change
@@ -1,79 +1,85 @@
'use strict';

(function() {
function List() {
this._listNoteContent = [];
this._wrapper = document.querySelector('#note-list-wrapper');
}

var _listNoteContent = [];
var _wrapper = document.querySelector('#note-list-wrapper');
List.prototype = {
Copy link
Contributor

Choose a reason for hiding this comment

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

You can re-write the chain as

this.fetchList().then(this.updateList.bind(this))
                      .then(this.drawList.bind(this))

It's more concise.

start : function () {
this.fetchList().then(function (data){
this.updateList(data);
}.bind(this)).then(
this.drawList.bind(this)
).then(
this.preloadFirstNote.bind(this)
);
window.addEventListener('click', (function (event) {
this.onNoteOpen(event);
}).bind(this));
},

function start() {
fetchList(function(data) {
updateList(data);
drawList();
preloadFirstNote();
});
window.addEventListener('click', function(event) {
onNoteOpen(event);
});
}
onNoteOpen : function (event) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Well it's a convention to prefix with on to handle the corresponding event, rather than to fire it. So, you should rename this function as onClick and to dispatch the note-open.

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 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 }));
};
}
preloadFirstNote : function () {
if (this._listNoteContent.length !== 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: It's better to put the condition outside a function wholly wrapped by only one branch.

var content = this._listNoteContent[0];
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 }));
}
}
updateList : function (list) {
this._listNoteContent = list;
},

function updateList(list) {
_listNoteContent = list;
}
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 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);
}
fetchList : function () {
return new Promise(function (resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://127.0.0.1:8000/homework/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();
});
}
};
var list = new List();
document.addEventListener('DOMContentLoaded', function(event) {
list.start();
});

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();
});

})();
11 changes: 7 additions & 4 deletions homework/test/test-list.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
describe('Test > ', function() {
beforeEach(function() {
describe('Test Math', function () {
var testAdd;
beforeEach(function () {
testAdd = new Content();
});

it('will test some pure functions', function() {
// Write any pure function assertion here.
it('can test addition', function() {
var sum = testAdd.addition(5, 5);
assert.equal(sum, 5 + 5);
});
});