Skip to content

Commit 698f805

Browse files
committed
Update hacker news demo for the v2.2.x and replace vue-resource to axios for the v2.0.0+
1 parent 5c70f9f commit 698f805

File tree

1 file changed

+74
-5
lines changed

1 file changed

+74
-5
lines changed

src/components/HackerNews.vue

Lines changed: 74 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
<p>Before create it, we need to prepare the following things:</p>
55
<ol>
66
<li>Hacker News API, we used <a target="_blank" href="https://hn.algolia.com/api">HN Search API</a> in this case;</li>
7-
<li>Import <a target="_blank" href="https://github.com/vuejs/vue-resource">vue-resource</a> plugin to request news data.</li>
7+
<li v-show="$parent.docVersion < 2">Import the <a target="_blank" href="https://github.com/vuejs/vue-resource">vue-resource</a> plugin to request news data.</li>
8+
<li v-show="$parent.docVersion >= 2">Import the <a target="_blank" href="https://github.com/mzabriskie/axios">axios</a> library to request news data.</li>
89
</ol>
910
<h4>Template</h4>
1011
<pre v-highlightjs v-if="$parent.docVersion < 2">&lt;div class=&quot;hacker-news-list&quot;&gt;
@@ -37,7 +38,7 @@
3738
&lt;/span&gt;
3839
&lt;/infinite-loading&gt;
3940
&lt;/div&gt;</pre>
40-
<pre v-highlightjs v-if="$parent.docVersion >= 2">&lt;div class=&quot;hacker-news-list&quot;&gt;
41+
<pre v-highlightjs v-if="$parent.docVersion >= 2 && $parent.docVersion < 2.2">&lt;div class=&quot;hacker-news-list&quot;&gt;
4142
&lt;div class=&quot;hacker-news-header&quot;&gt;
4243
&lt;a target=&quot;_blank&quot; href=&quot;http://www.ycombinator.com/&quot;&gt;
4344
&lt;img src=&quot;https://news.ycombinator.com/y18.gif&quot;&gt;
@@ -66,6 +67,36 @@
6667
There is no more Hacker News :(
6768
&lt;/span&gt;
6869
&lt;/infinite-loading&gt;
70+
&lt;/div&gt;</pre>
71+
<pre v-highlightjs v-if="$parent.docVersion >= 2.2">&lt;div class=&quot;hacker-news-list&quot;&gt;
72+
&lt;div class=&quot;hacker-news-header&quot;&gt;
73+
&lt;a target=&quot;_blank&quot; href=&quot;http://www.ycombinator.com/&quot;&gt;
74+
&lt;img src=&quot;https://news.ycombinator.com/y18.gif&quot;&gt;
75+
&lt;/a&gt;
76+
&lt;span&gt;Hacker News&lt;/span&gt;
77+
&lt;/div&gt;
78+
&lt;div class=&quot;hacker-news-item&quot; v-for=&quot;(item, key) in list&quot;&gt;
79+
&lt;span class=&quot;num&quot; v-text=&quot;key + 1&quot;&gt;&lt;/span&gt;
80+
&lt;p&gt;
81+
&lt;a target=&quot;_blank&quot; :href=&quot;item.url&quot; v-text=&quot;item.title&quot;&gt;&lt;/a&gt;
82+
&lt;/p&gt;
83+
&lt;p&gt;
84+
&lt;small&gt;
85+
&lt;span v-text=&quot;item.points&quot;&gt;&lt;/span&gt;
86+
points by
87+
&lt;a target=&quot;_blank&quot; :href=&quot;'https://news.ycombinator.com/user?id=' + item.author&quot;
88+
v-text=&quot;item.author&quot;&gt;&lt;/a&gt;
89+
|
90+
&lt;a target=&quot;_blank&quot; :href=&quot;'https://news.ycombinator.com/item?id=' + item.objectID&quot;
91+
v-text=&quot;item.num_comments + ' comments'&quot;&gt;&lt;/a&gt;
92+
&lt;/small&gt;
93+
&lt;/p&gt;
94+
&lt;/div&gt;
95+
&lt;infinite-loading @infinite=&quot;infiniteHandler&quot;&gt;
96+
&lt;span slot=&quot;no-more&quot;&gt;
97+
There is no more Hacker News :(
98+
&lt;/span&gt;
99+
&lt;/infinite-loading&gt;
69100
&lt;/div&gt;</pre>
70101
<p>In the template, we create a header and a list for Hacker News. For <code>InfiniteLoading</code> component in this case, unlike the basic use, we customized the content of the no more data prompt base on <code>slot</code>.</p>
71102
<h4>Script</h4>
@@ -131,7 +162,8 @@ export default {
131162
InfiniteLoading,
132163
},
133164
};</pre>
134-
<pre v-highlightjs v-if="$parent.docVersion >= 2">import InfiniteLoading from 'vue-infinite-loading';
165+
<pre v-highlightjs v-if="$parent.docVersion >= 2 && $parent.docVersion < 2.2">import InfiniteLoading from 'vue-infinite-loading';
166+
import axios from 'axios';
135167

136168
const api = 'http://hn.algolia.com/api/v1/search_by_date?tags=story';
137169

@@ -143,7 +175,7 @@ export default {
143175
},
144176
methods: {
145177
onInfinite() {
146-
this.$http.get(api, {
178+
axios.get(api, {
147179
params: {
148180
page: this.list.length / 20 + 1,
149181
},
@@ -163,8 +195,45 @@ export default {
163195
components: {
164196
InfiniteLoading,
165197
},
198+
};</pre>
199+
<pre v-highlightjs v-if="$parent.docVersion >= 2.2">import InfiniteLoading from 'vue-infinite-loading';
200+
import axios from 'axios';
201+
202+
const api = 'http://hn.algolia.com/api/v1/search_by_date?tags=story';
203+
204+
export default {
205+
data() {
206+
return {
207+
list: [],
208+
};
209+
},
210+
methods: {
211+
infiniteHandler($state) {
212+
axios.get(api, {
213+
params: {
214+
page: this.list.length / 20 + 1,
215+
},
216+
}).then(res =&gt; res.json()).then((data) =&gt; {
217+
if (data.hits.length) {
218+
this.list = this.list.concat(data.hits);
219+
$state.loaded();
220+
if (this.list.length / 20 === 10) {
221+
$state.complete();
222+
}
223+
} else {
224+
$state.complete();
225+
}
226+
});
227+
},
228+
},
229+
components: {
230+
InfiniteLoading,
231+
},
166232
};</pre>
167233
<p>
168-
In the <code>onInfinite</code> function, we request a page of news and pushed them into list everytime, if we had been requested 10 pages of news, send an <code v-show="$parent.docVersion < 1">$InfiniteLoading:noMore</code><code v-show="$parent.docVersion >= 1">$InfiniteLoading:complete</code> event to tell the <code>InfiniteLoading</code> component that there is no more data now, it will display the no more data prompt that we customized in template for user.
234+
In the <code>onInfinite</code> function, we request a page of news and pushed them into list everytime, if we had been requested 10 pages of news,
235+
<span v-show="$parent.docVersion < 2.2">send an <code v-show="$parent.docVersion < 1">$InfiniteLoading:noMore</code><code v-show="$parent.docVersion >= 1">$InfiniteLoading:complete</code> event</span>
236+
<span v-show="$parent.docVersion >= 2.2">call the <code>complete</code> method of the <code>$state</code> special event argument</span>
237+
to tell the <code>InfiniteLoading</code> component that there is no more data now, it will display the no more data prompt that we customized in template for user.
169238
</p>
170239
</template>

0 commit comments

Comments
 (0)