Skip to content

Commit 360bba8

Browse files
committed
docs: add en and zh hacker news example
1 parent 5894ba3 commit 360bba8

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed

docs/.vuepress/config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ module.exports = {
2020
sidebar: {
2121
'/guide/': [
2222
'',
23+
'start-with-hn',
2324
],
2425
},
2526
footer: `
@@ -44,6 +45,7 @@ module.exports = {
4445
sidebar: {
4546
'/zh/guide/': [
4647
'',
48+
'start-with-hn',
4749
],
4850
},
4951
footer: `

docs/guide/start-with-hn.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
---
2+
previewLink: //jsfiddle.net/PeachScript/2edu1k7o/embedded/result/
3+
---
4+
5+
# Start With Hacker News
6+
7+
As the first step in learning this plugin, we will create a infinite scroll version of the [Hacker News](https://news.ycombinator.com/).
8+
9+
Firstly, we need to write template, it similar to this (ommited unimportant code):
10+
11+
``` html
12+
<header>
13+
<!-- Hacker News header -->
14+
</header>
15+
16+
<div v-for="(item, key) in list">
17+
<!-- Hacker News item loop -->
18+
</div>
19+
20+
<infinite-loading @infinite="infiniteHandler"></infinite-loading>
21+
```
22+
23+
In the template, we put the `InfiniteLoading` component at the bottom of news list, and listen it's `infinite` event by a method called `infiniteHandler`.
24+
25+
Then write the core logic, `infiniteHandler` method:
26+
27+
``` js
28+
import axios from 'axios';
29+
30+
const api = '//hn.algolia.com/api/v1/search_by_date?tags=story';
31+
32+
export default {
33+
data() {
34+
return {
35+
page: 1,
36+
list: [],
37+
};
38+
},
39+
methods: {
40+
infiniteHandler($state) {
41+
axios.get(api, {
42+
params: {
43+
page: this.page,
44+
},
45+
}).then(({ data }) => {
46+
if (data.hits.length) {
47+
this.page += 1;
48+
this.list = this.list.concat(data.hits);
49+
$state.loaded();
50+
} else {
51+
$state.complete();
52+
}
53+
});
54+
},
55+
},
56+
};
57+
```
58+
59+
In the script, we use [HN Search API](https://hn.algolia.com/api) and [axios](https://github.com/mzabriskie/axios) to fetch news data. If the server API returns an array with news data, we will push them into `list`, record current page, and tell this plugin through `$state.loaded` method that we got some data. If the server API returns an empty array, we will tell this plugin through `$state.complete` method that all data has been loaded.
60+
61+
Now, you can get an infinite scroll version of Hacker News, just like the preview on the right.

docs/zh/guide/start-with-hn.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
---
2+
previewLink: //jsfiddle.net/PeachScript/2edu1k7o/embedded/result/
3+
---
4+
5+
# 从 Hacker News 开始
6+
7+
作为了解这款插件的第一步,我们将会创建一个无限滚动版的 [Hacker News](https://news.ycombinator.com/)
8+
9+
首先,我们需要编写模板,内容大概如下(已省略不重要的代码):
10+
11+
``` html
12+
<header>
13+
<!-- Hacker News header -->
14+
</header>
15+
16+
<div v-for="(item, key) in list">
17+
<!-- Hacker News item loop -->
18+
</div>
19+
20+
<infinite-loading @infinite="infiniteHandler"></infinite-loading>
21+
```
22+
23+
在模板中,我们将 `InfiniteLoading` 组件放在了新闻列表的最下方,并且使用一个叫做 `infiniteHandler` 的方法监听组件的 `infinite` 事件。
24+
25+
接下来编写核心逻辑—— `infiniteHandler` 方法:
26+
27+
``` js
28+
import axios from 'axios';
29+
30+
const api = '//hn.algolia.com/api/v1/search_by_date?tags=story';
31+
32+
export default {
33+
data() {
34+
return {
35+
page: 1,
36+
list: [],
37+
};
38+
},
39+
methods: {
40+
infiniteHandler($state) {
41+
axios.get(api, {
42+
params: {
43+
page: this.page,
44+
},
45+
}).then(({ data }) => {
46+
if (data.hits.length) {
47+
this.page += 1;
48+
this.list = this.list.concat(data.hits);
49+
$state.loaded();
50+
} else {
51+
$state.complete();
52+
}
53+
});
54+
},
55+
},
56+
};
57+
```
58+
59+
在这段脚本中,我们使用 [HN Search API](https://hn.algolia.com/api)[axios](https://github.com/mzabriskie/axios) 来获取新闻数据。如果服务端 API 返回了带新闻数据的数组,我们会将数据放入 `list`、会记录当前页码,并且通过 `$state.loaded` 方法通知插件我们已经拿到数据了;如果服务端 API 返回的是空数据,我们将会通过 `$state.complete` 方法通知插件所有数据都加载完了。
60+
61+
现在,你已经完成了一个无限滚动版本的 Hacker News, 就像右边的预览一样。

0 commit comments

Comments
 (0)