ajax调用方法重构

This commit is contained in:
结发受长生 2018-05-08 21:29:14 +08:00
parent ff62d72b4f
commit 939deaef3d
4 changed files with 45 additions and 129 deletions

View File

@ -22,7 +22,7 @@
<div class="icon"><span class="icon-search"></span></div> <div class="icon"><span class="icon-search"></span></div>
<div class="input-box"><input type="text" id="search-input" v-model="queryText" placeholder="站内搜索"/></div> <div class="input-box"><input type="text" id="search-input" v-model="queryText" placeholder="站内搜索"/></div>
<!-- 搜索结果区 --> <!-- 搜索结果区 -->
<div id="search-result-box" v-show="queryText"> <div id="search-result-box" >
<ul class="search-result-list" v-if="searchResult.length"> <ul class="search-result-list" v-if="searchResult.length">
<li v-for="(article,index) in searchResult" :key="index"> <li v-for="(article,index) in searchResult" :key="index">
<a :href='article.url' class='search-result-title' target='_blank'>{{article.title}}</a> <a :href='article.url' class='search-result-title' target='_blank'>{{article.title}}</a>

View File

@ -44,10 +44,11 @@
width 300px width 300px
max-height 600px max-height 600px
overflow-y auto overflow-y auto
background #eee background #f3f3f3
border 1px solid #333 box-shadow: -2px 3px 9px 0px #939393;
border-radius 5px border-radius 5px
word-break break-all word-break break-all
display none
padding 7px padding 7px
ul ul
list-style none list-style none
@ -55,7 +56,7 @@
margin-top 0 margin-top 0
a.search-result-title a.search-result-title
color #3272b5 color #3272b5
font-size 18px font-size 16px
p.search-result p.search-result
font-size 14px font-size 14px
margin 0.6em 0 margin 0.6em 0

View File

@ -709,6 +709,18 @@ var JELON = window.JELON || {};
$('JELON__previewBox').innerHTML = ''; $('JELON__previewBox').innerHTML = '';
} }
}; };
var createSuccessCb = function(callback){
return function (res) {
if (typeof res === 'string') {
if (window.JSON) {
res = JSON.parse(res);
} else {
res = eval('(' + res + ')');
}
}
callback && callback(res);
}
}
JL.Requests = { JL.Requests = {
getIssueNumberByLabel: function (label, callback) { getIssueNumberByLabel: function (label, callback) {
ajax({ ajax({
@ -718,19 +730,8 @@ var JELON = window.JELON || {};
labels: [ label ], labels: [ label ],
rnd: Math.random() rnd: Math.random()
}, },
success: function (res) { success: createSuccessCb(callback),
if (typeof res === 'string') { fail: callback
if (window.JSON) {
res = JSON.parse(res);
} else {
res = eval('(' + res + ')');
}
}
callback && callback(res);
},
fail: function (err) {
callback && callback(err);
}
}); });
}, },
createIssue: function (data, callback) { createIssue: function (data, callback) {
@ -738,19 +739,8 @@ var JELON = window.JELON || {};
url: constants.API_HOST + '/repos/' + JL.options.owner + '/' + JL.options.repo + '/issues', url: constants.API_HOST + '/repos/' + JL.options.owner + '/' + JL.options.repo + '/issues',
method: 'POST', method: 'POST',
data: data, data: data,
success: function (res) { success: createSuccessCb(callback),
if (typeof res === 'string') { fail: callback
if (window.JSON) {
res = JSON.parse(res);
} else {
res = eval('(' + res + ')');
}
}
callback && callback(res);
},
fail: function (err) {
callback && callback(err);
}
}); });
}, },
getCommentListByIssueNumber: function (number, data, callback) { getCommentListByIssueNumber: function (number, data, callback) {
@ -758,19 +748,8 @@ var JELON = window.JELON || {};
url: constants.API_HOST + '/repos/' + JL.options.owner + '/' + JL.options.repo + '/issues/' + number + '/comments', url: constants.API_HOST + '/repos/' + JL.options.owner + '/' + JL.options.repo + '/issues/' + number + '/comments',
method: 'GET', method: 'GET',
data: data, data: data,
success: function (res) { success: createSuccessCb(callback),
if (typeof res === 'string') { fail: callback
if (window.JSON) {
res = JSON.parse(res);
} else {
res = eval('(' + res + ')');
}
}
callback && callback(res);
},
fail: function (err) {
callback && callback(err);
}
}); });
}, },
getReactionsByCommentId: function (id, data, callback) { getReactionsByCommentId: function (id, data, callback) {
@ -781,19 +760,8 @@ var JELON = window.JELON || {};
url: constants.API_HOST + '/repos/' + JL.options.owner + '/' + JL.options.repo + '/issues/comments/' + id + '/reactions', url: constants.API_HOST + '/repos/' + JL.options.owner + '/' + JL.options.repo + '/issues/comments/' + id + '/reactions',
method: 'GET', method: 'GET',
data: data, data: data,
success: function (res) { success: createSuccessCb(callback),
if (typeof res === 'string') { fail: callback
if (window.JSON) {
res = JSON.parse(res);
} else {
res = eval('(' + res + ')');
}
}
callback && callback(res);
},
fail: function (err) {
callback && callback(err);
}
}); });
}, },
editIssue: function (number, data, callback) { editIssue: function (number, data, callback) {
@ -801,19 +769,8 @@ var JELON = window.JELON || {};
url: constants.API_HOST + '/repos/' + JL.options.owner + '/' + JL.options.owner + '/issues/' + number, url: constants.API_HOST + '/repos/' + JL.options.owner + '/' + JL.options.owner + '/issues/' + number,
method: 'POST', method: 'POST',
data: data, data: data,
success: function (res) { success: createSuccessCb(callback),
if (typeof res === 'string') { fail: callback
if (window.JSON) {
res = JSON.parse(res);
} else {
res = eval('(' + res + ')');
}
}
callback && callback(res);
},
fail: function (err) {
callback && callback(err);
}
}); });
}, },
markdown: function (data, callback) { markdown: function (data, callback) {
@ -821,12 +778,8 @@ var JELON = window.JELON || {};
url: constants.API_HOST + '/markdown', url: constants.API_HOST + '/markdown',
method: 'POST', method: 'POST',
data: data, data: data,
success: function (res) { success: callback,
callback && callback(res); fail: callback
},
fail: function (err) {
callback && callback(err);
}
}); });
}, },
getAccessToken: function (data, callback) { getAccessToken: function (data, callback) {
@ -838,19 +791,8 @@ var JELON = window.JELON || {};
'Accept': 'application/json' 'Accept': 'application/json'
}, },
data: data, data: data,
success: function (res) { success: createSuccessCb(callback),
if (typeof res === 'string') { fail: callback
if (window.JSON) {
res = JSON.parse(res);
} else {
res = eval('(' + res + ')');
}
}
callback && callback(res);
},
fail: function (err) {
callback && callback(err);
}
}); });
}, },
getUserInfo: function (data, callback) { getUserInfo: function (data, callback) {
@ -858,19 +800,8 @@ var JELON = window.JELON || {};
url: constants.API_HOST + '/user', url: constants.API_HOST + '/user',
method: 'GET', method: 'GET',
data: data, data: data,
success: function (res) { success: createSuccessCb(callback),
if (typeof res === 'string') { fail: callback
if (window.JSON) {
res = JSON.parse(res);
} else {
res = eval('(' + res + ')');
}
}
callback && callback(res);
},
fail: function (err) {
callback && callback(err);
}
}); });
}, },
createComment: function (number, data, callback) { createComment: function (number, data, callback) {
@ -878,19 +809,8 @@ var JELON = window.JELON || {};
url: constants.API_HOST + '/repos/' + JL.options.owner + '/' + JL.options.repo + '/issues/' + number + '/comments', url: constants.API_HOST + '/repos/' + JL.options.owner + '/' + JL.options.repo + '/issues/' + number + '/comments',
method: 'POST', method: 'POST',
data: data, data: data,
success: function (res) { success: createSuccessCb(callback),
if (typeof res === 'string') { fail: callback
if (window.JSON) {
res = JSON.parse(res);
} else {
res = eval('(' + res + ')');
}
}
callback && callback(res);
},
fail: function (err) {
callback && callback(err);
}
}); });
}, },
createReaction: function (commentId, data, callback) { createReaction: function (commentId, data, callback) {
@ -898,19 +818,8 @@ var JELON = window.JELON || {};
url: constants.API_HOST + '/repos/' + JL.options.owner + '/' + JL.options.repo + '/issues/comments/' + commentId + '/reactions', url: constants.API_HOST + '/repos/' + JL.options.owner + '/' + JL.options.repo + '/issues/comments/' + commentId + '/reactions',
method: 'POST', method: 'POST',
data: data, data: data,
success: function (res) { success: createSuccessCb(callback),
if (typeof res === 'string') { fail: callback
if (window.JSON) {
res = JSON.parse(res);
} else {
res = eval('(' + res + ')');
}
}
callback && callback(res);
},
fail: function (err) {
callback && callback(err);
}
}); });
} }
}; };

View File

@ -1,5 +1,6 @@
(function(){ (function(){
var articleDatas = null; var articleDatas = null;
var resultDiv = null;
new Vue({ new Vue({
el: "#search-box", el: "#search-box",
data: { data: {
@ -27,12 +28,18 @@ new Vue({
url: item.getElementsByTagName("url")[0].innerHTML, url: item.getElementsByTagName("url")[0].innerHTML,
} }
}) })
resultDiv = document.getElementById("search-result-box")
}); });
}, },
watch: { watch: {
queryText: function(newVal, oldVal) { queryText: function(newVal, oldVal) {
this.searchResult.length = 0; this.searchResult.length = 0;
if(!newVal || !newVal.trim() || !articleDatas) return if(newVal && newVal.trim() && articleDatas) {
resultDiv.style.display = "block"
} else {
resultDiv.style.display = "none"
return
}
var keywords = newVal.trim().toLowerCase().split(/[\s\-]+/); var keywords = newVal.trim().toLowerCase().split(/[\s\-]+/);
var _this = this; var _this = this;
articleDatas.forEach(function(article){ articleDatas.forEach(function(article){
@ -76,7 +83,6 @@ new Vue({
end = content.length; end = content.length;
} }
var matchContent = content.substring(start, end); var matchContent = content.substring(start, end);
debugger
// 高亮关键字 // 高亮关键字
keywords.forEach(function(keyword){ keywords.forEach(function(keyword){
var keywordReg = new RegExp(keyword, "gi"); var keywordReg = new RegExp(keyword, "gi");