Skip to content

Commit cfc9ee4

Browse files
committed
Updated sample to load more items from the Reddit API.
1 parent 9d8858b commit cfc9ee4

File tree

2 files changed

+27
-33
lines changed

2 files changed

+27
-33
lines changed

samples/app/app-with-list-view.js

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Vue.prototype.$http = http
1212
new Vue({
1313
data: {
1414
subreddit: '/r/funny',
15+
page_num: 1,
16+
last_page: '',
1517
items: []
1618
},
1719

@@ -24,18 +26,14 @@ new Vue({
2426
<stack-layout orientation="horizontal" class="list-group-item">
2527
<image :src="item.image" class="thumb"></image>
2628
<stack-layout>
27-
<label class="list-group-item-heading" :text="item.title" textWrap="true"></label>
28-
<label class="list-group-item-text" text="The rest of the content" textWrap="true"></label>
29+
<label class="list-group-item-heading" :text="item.title" text-wrap="true"></label>
30+
<label class="list-group-item-text" text="The rest of the content" text-wrap="true"></label>
2931
</stack-layout>
3032
</stack-layout>
3133
</template>
32-
<template name="active" scope="item">
34+
<template name="page" scope="item">
3335
<stack-layout orientation="horizontal" class="list-group-item active">
34-
<image :src="item.image" class="thumb"></image>
35-
<stack-layout>
36-
<label class="list-group-item-heading" :text="item.title" textWrap="true"></label>
37-
<label class="list-group-item-text" text="The rest of the content" textWrap="true"></label>
38-
</stack-layout>
36+
<label :text="item.title"></label>
3937
</stack-layout>
4038
</template>
4139
</list-view>
@@ -80,26 +78,12 @@ new Vue({
8078
},
8179

8280
onLoadMoreItems(e) {
83-
console.log('Loading (3) more items')
84-
this.items.push({
85-
title: 'Foo loaded @ ' + new Date().toTimeString(),
86-
image: this.items[0].image,
87-
fullImage: this.items[0].fullImage
88-
},
89-
{
90-
title: 'Bar loaded @ ' + new Date().toTimeString(),
91-
image: this.items[1].image,
92-
fullImage: this.items[1].fullImage
93-
},
94-
{
95-
title: 'Baz loaded @ ' + new Date().toTimeString(),
96-
image: this.items[2].image,
97-
fullImage: this.items[2].fullImage
98-
})
81+
console.log('Loading more items')
82+
return this.fetchItems()
9983
},
10084

10185
templateSelector(item) {
102-
return item.$index === 0 ? 'active' : 'default'
86+
return item.type === 'page' ? 'page' : 'default'
10387
},
10488

10589
chooseSubreddit() {
@@ -117,14 +101,23 @@ new Vue({
117101
},
118102

119103
fetchItems() {
120-
this.$http.getJSON(`https://www.reddit.com/${this.subreddit}.json`).then((res) => {
121-
this.items = res.data.children.map((item) => {
122-
return {
104+
this.$http.getJSON(`https://www.reddit.com/${this.subreddit}.json?limit=10&count=10&after=${this.last_page}`).then((res) => {
105+
this.items.push({
106+
title: 'Page ' + this.page_num,
107+
type: 'page'
108+
})
109+
res.data.children.forEach((item) => {
110+
this.items.push({
123111
title: item.data.title,
124112
image: item.data.thumbnail,
125-
fullImage: item.data.preview.images[0].source.url
126-
}
113+
fullImage: item.data.preview.images[0].source.url,
114+
type: 'entry'
115+
})
127116
})
117+
this.last_page = res.data.after
118+
this.page_num++;
119+
120+
console.log('Loaded more items')
128121
}).catch((err) => {
129122
console.log('err..' + err)
130123
})

samples/package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@
55
"repository": "<fill-your-repository-here>",
66
"nativescript": {
77
"id": "org.nativescript.vuesample",
8-
"tns-android": {
9-
"version": "3.0.0"
10-
},
118
"tns-ios": {
129
"version": "3.0.1"
10+
},
11+
"tns-android": {
12+
"version": "3.0.1"
1313
}
1414
},
1515
"dependencies": {
1616
"nativescript-theme-core": "~1.0.2",
17+
"nativescript-vue": "^0.1.3",
1718
"tns-core-modules": "^3.0.0",
1819
"vue-router": "^2.4.0"
1920
},

0 commit comments

Comments
 (0)