Add server and mini-program pagination for shared lists
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
const { itemsOf, startList, applyList, failList, finishList, withListPagination } = require('../../utils/list-pagination');
|
||||
const { withDataCache } = require('../../utils/page-cache');
|
||||
const { getLayout } = require('../../utils/layout');
|
||||
// Diary journal with server pagination.
|
||||
const { api, getImageUrl } = require('../../utils/request');
|
||||
const { diaryEntry } = require('../../utils/journal');
|
||||
const { saveScrollPosition, getScrollPosition } = require('../../utils/scroll-pos');
|
||||
Page(withDataCache({
|
||||
Page(withDataCache(withListPagination({
|
||||
onResize: function() { this.setData(getLayout()); },
|
||||
data: {
|
||||
loading: true, loadError: false,
|
||||
@@ -51,45 +52,14 @@ Page(withDataCache({
|
||||
return { title: '让所有的爱有迹可循', imageUrl: '/images/share.png' };
|
||||
},
|
||||
|
||||
loadData: async function() {
|
||||
if (this._loading) return;
|
||||
this._loading = true;
|
||||
this.setData({ loading: !this._hasCachedData, loadError: false });
|
||||
loadData: async function(page) {
|
||||
const ticket=startList(this,page);if(!ticket)return;
|
||||
try {
|
||||
var data = await api.diaryList({ page: 1, page_size: this.data.pageSize });
|
||||
var items = this._formatItems(data.items || []);
|
||||
this.setData({
|
||||
page: 1,
|
||||
list: items,
|
||||
total: data.total || 0,
|
||||
hasMore: (data.total || 0) > this.data.pageSize
|
||||
});
|
||||
const data=await api.diaryList({page:ticket.page,page_size:20});
|
||||
applyList(this,ticket,data,this._formatItems(itemsOf(data)),'list');
|
||||
this._loadPartnerInfo();
|
||||
} catch (e) {
|
||||
this.setData({ loadError: true });
|
||||
console.error(e);
|
||||
wx.showToast({ title: (e && (e.error || e.errMsg || e.message)) || '加载失败', icon: 'none' });
|
||||
} finally { this._loading = false; this.setData({ loading: false }); }
|
||||
},
|
||||
|
||||
loadMore: async function() {
|
||||
if (this.data.loadingMore || !this.data.hasMore) return;
|
||||
this.setData({ loadingMore: true });
|
||||
try {
|
||||
var nextPage = this.data.page + 1;
|
||||
var data = await api.diaryList({ page: nextPage, page_size: this.data.pageSize });
|
||||
var newItems = this._formatItems(data.items || []);
|
||||
var merged = this.data.list.concat(newItems);
|
||||
this.setData({
|
||||
list: merged,
|
||||
page: nextPage,
|
||||
hasMore: merged.length < (data.total || 0),
|
||||
loadingMore: false
|
||||
});
|
||||
this._loadPartnerInfo();
|
||||
} catch (e) {
|
||||
this.setData({ loadingMore: false });
|
||||
}
|
||||
} catch(e) { failList(this,ticket); }
|
||||
finally { finishList(this,ticket); }
|
||||
},
|
||||
|
||||
_formatItems: function(items) {
|
||||
@@ -194,4 +164,4 @@ Page(withDataCache({
|
||||
goHome: function() { this._savePosBeforeLeave(); wx.switchTab({ url: '/pages/index/index' }); },
|
||||
goBack: function() { wx.navigateBack(); },
|
||||
goProfile: function() { this._savePosBeforeLeave(); wx.switchTab({ url: '/pages/profile/profile' }); }
|
||||
}, {"loadData": ["diaryList", "coupleInfo"]}));
|
||||
}, 'loadData'), {"loadData": ["diaryList", "coupleInfo"]}));
|
||||
|
||||
Reference in New Issue
Block a user