Skip to content

Commit fafecb8

Browse files
committed
Merge branch 'master' of github.com:shinygang/Vue-cnodejs
2 parents 704fb83 + e8e24b7 commit fafecb8

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#基于vue.js重写Cnodejs.org社区的webapp [线上访问地址](http://shinygang.github.io/cnodevue/)
1+
#基于vue.js重写Cnodejs.org社区的webapp [线上访问地址](http://shinygang.coding.me/)
22

33
## 注意:当前项目代码已经升级到2.0,采用vue-cli构建的2.0版本。1.x代码请看1.x分支。
44

src/views/list.vue

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<section id="page">
1111
<!-- 首页列表 -->
1212
<ul class="posts-list">
13-
<li v-for="item in topics">
13+
<li v-for="item in topics" :key="item.id">
1414
<router-link :to="{name:'topic',params:{id:item.id}}">
1515
<h3 v-text="item.title"
1616
:class="getTabInfo(item.tab, item.good, item.top, true)"
@@ -58,6 +58,7 @@
5858
return {
5959
scroll: true,
6060
topics: [],
61+
index: {},
6162
searchKey: {
6263
page: 1,
6364
limit: 20,
@@ -144,17 +145,26 @@
144145
$.get('https://cnodejs.org/api/v1/topics?' + params, (d) => {
145146
this.scroll = true;
146147
if (d && d.data) {
147-
this.topics = d.data;
148+
d.data.forEach(this.mergeTopics);
148149
}
149150
});
150151
},
152+
mergeTopics(topic) {
153+
if (typeof this.index[topic.id] === 'number') {
154+
const topicsIndex = this.index[topic.id];
155+
this.topics[topicsIndex] = topic;
156+
} else {
157+
this.index[topic.id] = this.topics.length;
158+
this.topics.push(topic);
159+
}
160+
},
151161
// 滚动加载数据
152162
getScrollData() {
153163
if (this.scroll) {
154164
let totalheight = parseInt($(window).height(), 20) + parseInt($(window).scrollTop(), 20);
155165
if ($(document).height() <= totalheight + 200) {
156166
this.scroll = false;
157-
this.searchKey.limit += 20;
167+
this.searchKey.page += 1;
158168
this.getTopics();
159169
}
160170
}
@@ -166,8 +176,10 @@
166176
// 如果是当前页面切换分类的情况
167177
if (to.query && to.query.tab) {
168178
this.searchKey.tab = to.query.tab;
179+
this.topics = [];
180+
this.index = {};
169181
}
170-
this.searchKey.limit = 20;
182+
this.searchKey.page = 1;
171183
this.getTopics();
172184
// 隐藏导航栏
173185
this.$refs.head.show = false;
@@ -178,4 +190,4 @@
178190
nvTop
179191
}
180192
};
181-
</script>
193+
</script>

0 commit comments

Comments
 (0)