diff --git a/homework/content.js b/homework/content.js
index 710be28..7656ace 100644
--- a/homework/content.js
+++ b/homework/content.js
@@ -1,21 +1,26 @@
'use strict';
-(function() {
- var _wrapper = document.querySelector('#note-content-wrapper');
+(function(exports) {
+ var ContentManager = function(){
+ this._wrapper = null;
+};
- function start() {
- window.addEventListener('note-open', function(event) {
- var note = event.detail;
- resetWrapper();
- drawNote(note);
- });
- }
+ContentManager.prototype = {
- function resetWrapper() {
- _wrapper.innerHTML = '';
+ start() {
+ this._wrapper = document.querySelector('#note-content-wrapper');
+ window.addEventListener('note-open', (function(event){
+ var note = event.detail;
+ this.resetWrapper();
+ this.drawNote(note);
+ }).bind(this));
}
-
- function drawNote(note) {
+
+ resetWrapper() {
+ this._wrapper.innerHTML = '';
+ },
+
+ drawNote(note) {
var title = note.title;
var h = document.createElement('h2');
h.textContent = title;
@@ -27,11 +32,10 @@
p.textContent = passage;
buff.appendChild(p);
});
- _wrapper.appendChild(h);
- _wrapper.appendChild(buff);
- }
-
- document.addEventListener('DOMContentLoaded', function(event) {
- start();
- });
-})();
+ this._wrapper.appendChild(h);
+ this._wrapper.appendChild(buff);
+ },
+};
+
+ exports.ContentManager = ContentManager;
+})(window);
\ No newline at end of file
diff --git a/homework/index.html b/homework/index.html
index e387963..ad28075 100644
--- a/homework/index.html
+++ b/homework/index.html
@@ -3,6 +3,7 @@
Homework - Note List
+
diff --git a/homework/list.js b/homework/list.js
index 21eddbc..55f7cf7 100644
--- a/homework/list.js
+++ b/homework/list.js
@@ -1,44 +1,49 @@
'use strict';
-(function() {
+(function(exports) {
+var ListManager = function(){
+ this._listNoteContent = [];
+ this._wrapper = null;
+};
- 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) {
+ListManager.prototype = {
+
+ start() {
+ this._wrapper = document.querySelector('#note-list-wrapper');
+ this.fetchList()
+ .then((function(data) {
+ this.updateList(data);
+ this.drawList();
+ this.preloadFirstNote();
+ }).bind(this));
+ window.addEventListener('click', (function(event) {
+ this.onNoteOpen(event);
+ }).bind(this));
+ }
+
+ 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,10 +57,11 @@
buff.appendChild(li);
});
ul.appendChild(buff);
- _wrapper.appendChild(ul);
- }
+ this._wrapper.appendChild(ul);
+ },
- function fetchList(afterFetch) {
+ fetchList(afterFetch) {
+ 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';
@@ -64,16 +70,16 @@
if (this.readyState === 4 && this.status === 200) {
var listData = this.response;
// The flow ends here.
- afterFetch(listData);
+ resolve(listData);
} else if (this.status !== 200 ){
// Ignore error in this case.
+ reject('FETCHING FAILED: ' + this.status + ' ' + this.readyState);
}
};
xhr.send();
- }
-
- document.addEventListener('DOMContentLoaded', function(event) {
- start();
- });
+ }).bind(this));
+ },
+};
-})();
+ exports.ListManager = ListManager;
+})(window);
\ No newline at end of file
diff --git a/homework/main.js b/homework/main.js
new file mode 100644
index 0000000..ba24689
--- /dev/null
+++ b/homework/main.js
@@ -0,0 +1,7 @@
+'use strict';
+document.addEventListener('DOMContentLoaded', function(event){
+ var listmanager = new ListManager();
+ var contentmanager = new ContentManager();
+ listmanager.start();
+ contentmanager.start();
+});
\ No newline at end of file
diff --git a/homework/test/test-list.js b/homework/test/test-list.js
index 441fc45..65761a3 100644
--- a/homework/test/test-list.js
+++ b/homework/test/test-list.js
@@ -1,8 +1,13 @@
-describe('Test > ', function() {
- beforeEach(function() {
- });
+var assert = require('assert');
+var Test = function(){};
- it('will test some pure functions', function() {
- // Write any pure function assertion here.
- });
-});
+Test.prototype.add = function (n1,n2){
+ return n1+n2;
+}
+
+ describe('add', function(){
+ it('add', function(){
+ var test = new Test();
+ assert.equal('10', test.add(7, 3));
+ })
+ })
\ No newline at end of file