From 0547ec5aa2be7bfc90fe67a31b72951324c95cb5 Mon Sep 17 00:00:00 2001 From: rr- Date: Sun, 12 Jun 2016 22:02:15 +0200 Subject: [PATCH] client/paging: remember results in history --- client/js/controllers/comments_controller.js | 14 ++++++++------ client/js/controllers/page_controller.js | 16 ++++++++++++++++ client/js/controllers/posts_controller.js | 16 +++++++++------- client/js/controllers/tags_controller.js | 14 ++++++++------ client/js/controllers/users_controller.js | 12 +++++++----- 5 files changed, 48 insertions(+), 24 deletions(-) diff --git a/client/js/controllers/comments_controller.js b/client/js/controllers/comments_controller.js index 61e23253..5244a3b2 100644 --- a/client/js/controllers/comments_controller.js +++ b/client/js/controllers/comments_controller.js @@ -23,12 +23,14 @@ class CommentsController { pageController.run({ searchQuery: ctx.searchQuery, clientUrl: '/comments/' + misc.formatSearchQuery({page: '{page}'}), - requestPage: page => { - return api.get( - '/posts/?query=sort:comment-date+comment-count-min:1' + - `&page=${page}&pageSize=10&fields=` + - 'id,comments,commentCount,thumbnailUrl'); - }, + requestPage: pageController.createHistoryCacheProxy( + ctx, + page => { + return api.get( + '/posts/?query=sort:comment-date+comment-count-min:1' + + `&page=${page}&pageSize=10&fields=` + + 'id,comments,commentCount,thumbnailUrl'); + }), pageRenderer: this._commentsPageView, pageContext: { canViewPosts: api.hasPrivilege('posts:view'), diff --git a/client/js/controllers/page_controller.js b/client/js/controllers/page_controller.js index 9f3b51da..20474841 100644 --- a/client/js/controllers/page_controller.js +++ b/client/js/controllers/page_controller.js @@ -37,6 +37,22 @@ class PageController { this._pageView.render(ctx); } + createHistoryCacheProxy(routerCtx, requestPage) { + return page => { + if (routerCtx.state.response) { + return new Promise((resolve, reject) => { + resolve(routerCtx.state.response); + }); + } + const promise = requestPage(page); + promise.then(response => { + routerCtx.state.response = response; + routerCtx.save(); + }); + return promise; + }; + } + stop() { this._pageView.unrender(); } diff --git a/client/js/controllers/posts_controller.js b/client/js/controllers/posts_controller.js index 296455d8..bf61fc22 100644 --- a/client/js/controllers/posts_controller.js +++ b/client/js/controllers/posts_controller.js @@ -48,13 +48,15 @@ class PostsController { searchQuery: ctx.searchQuery, clientUrl: '/posts/' + misc.formatSearchQuery({ text: ctx.searchQuery.text, page: '{page}'}), - requestPage: page => { - const text = this._decorateSearchQuery(ctx.searchQuery.text); - return api.get( - `/posts/?query=${text}&page=${page}&pageSize=40&fields=` + - `id,type,tags,score,favoriteCount,` + - `commentCount,thumbnailUrl`); - }, + requestPage: pageController.createHistoryCacheProxy( + ctx, + page => { + const text = this._decorateSearchQuery(ctx.searchQuery.text); + return api.get( + `/posts/?query=${text}&page=${page}&pageSize=40` + + '&fields=id,type,tags,score,favoriteCount,' + + 'commentCount,thumbnailUrl'); + }), headerRenderer: this._postsHeaderView, pageRenderer: this._postsPageView, pageContext: { diff --git a/client/js/controllers/tags_controller.js b/client/js/controllers/tags_controller.js index ed29c4a3..4083828b 100644 --- a/client/js/controllers/tags_controller.js +++ b/client/js/controllers/tags_controller.js @@ -207,12 +207,14 @@ class TagsController { searchQuery: ctx.searchQuery, clientUrl: '/tags/' + misc.formatSearchQuery({ text: ctx.searchQuery.text, page: '{page}'}), - requestPage: page => { - const text = ctx.searchQuery.text; - return api.get( - `/tags/?query=${text}&page=${page}&pageSize=50&fields=` + - `names,suggestions,implications,lastEditTime,usages`); - }, + requestPage: pageController.createHistoryCacheProxy( + ctx, + page => { + const text = ctx.searchQuery.text; + return api.get( + `/tags/?query=${text}&page=${page}&pageSize=50&fields=` + + `names,suggestions,implications,lastEditTime,usages`); + }), headerRenderer: this._tagsHeaderView, pageRenderer: this._tagsPageView, headerContext: { diff --git a/client/js/controllers/users_controller.js b/client/js/controllers/users_controller.js index a2e276ae..ecfc41f6 100644 --- a/client/js/controllers/users_controller.js +++ b/client/js/controllers/users_controller.js @@ -71,11 +71,13 @@ class UsersController { searchQuery: ctx.searchQuery, clientUrl: '/users/' + misc.formatSearchQuery({ text: ctx.searchQuery.text, page: '{page}'}), - requestPage: page => { - const text = ctx.searchQuery.text; - return api.get( - `/users/?query=${text}&page=${page}&pageSize=30`); - }, + requestPage: pageController.createHistoryCacheProxy( + ctx, + page => { + const text = ctx.searchQuery.text; + return api.get( + `/users/?query=${text}&page=${page}&pageSize=30`); + }), headerRenderer: this._usersHeaderView, pageRenderer: this._usersPageView, pageContext: {