From 0b4f2bb2b072d041720617eb6ee1b51ea41c20fa Mon Sep 17 00:00:00 2001 From: SukkaW Date: Sun, 7 Oct 2018 12:32:12 +0800 Subject: [PATCH] refactor --- src/disqus.js | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/src/disqus.js b/src/disqus.js index 5818083..7272bf2 100644 --- a/src/disqus.js +++ b/src/disqus.js @@ -127,7 +127,7 @@ function getThreadInfo() { * API URI: /3.0/posts/list.json?forum=[shortname]&thread=[thread id]&api_key=[apikey] */ - getComment = function () { + var getComment = function () { var url = disqusjs.config.api + '3.0/posts/list.json?forum=' + disqusjs.config.shortname + '&thread=' + disqusjs.page.id + '&api_key=' + disqusjs.config.apikey; xhr.open('GET', url, true); xhr.timeout = 4000; @@ -136,7 +136,7 @@ function getThreadInfo() { if (this.status == 200 || this.status == 304) { var res = JSON.parse(this.responseText); if (res.code === 0) { - renderCommentList(res.response); + getCommentList(res.response); } else { // Have error when get comments. } @@ -176,11 +176,11 @@ function getThreadInfo() { } /* - * Name: renderCommentList(data) + * Name: getCommentList(data) * Description: Render JSON to comment list components */ -function renderCommentList(data) { +function getCommentList(data) { var topLevelComments = []; var childComments = []; @@ -199,30 +199,37 @@ function renderCommentList(data) { function getChildren(id) { if (childComments.length === 0) { - return null + return null; } - var list = [] + var list = []; for (var i = 0; i < childComments.length; i++) { var comment = childComments[i]; if (comment.parent === id) { list.unshift({ - comment, + comment: comment, author: comment.author.name, isPrimary: comment.author.username === disqusjs.config.admin, - children: getChildren(+comment.id) - }) + children: getChildren(Number(comment.id)) + }); } } if (list.length) { - return list + return list; } else { - return null + return null; } } - console.log(commentLists) + renderComment(commentLists) +} + +function renderComment(data) { + data.map(function (comment) { + console.log(comment) + }) } +getThreadInfo(); \ No newline at end of file