From b3a065a4fb2a1a260fbaab79012b1d512a167371 Mon Sep 17 00:00:00 2001 From: winkidney Date: Thu, 5 Dec 2019 00:00:16 +0800 Subject: [PATCH] Feature: Add global event bus to trigger pin-refresh + Any other good idea to do this? --- pinry-spa/src/components/Pins.vue | 39 ++++++++++++++----- .../components/pin_create/PinCreateModal.vue | 2 + pinry-spa/src/components/utils/bus.js | 10 +++++ 3 files changed, 41 insertions(+), 10 deletions(-) create mode 100644 pinry-spa/src/components/utils/bus.js diff --git a/pinry-spa/src/components/Pins.vue b/pinry-spa/src/components/Pins.vue index 6937a3e..25cef37 100644 --- a/pinry-spa/src/components/Pins.vue +++ b/pinry-spa/src/components/Pins.vue @@ -70,6 +70,7 @@ import PinPreview from './PinPreview.vue'; import loadingSpinner from './loadingSpinner.vue'; import noMore from './noMore.vue'; import scroll from './utils/scroll'; +import bus from './utils/bus'; function createImageItem(pin) { const image = {}; @@ -92,6 +93,18 @@ function createImageItem(pin) { return image; } +function initialData() { + return { + blocks: [], + blocksMap: {}, + status: { + loading: false, + hasNext: true, + offset: 0, + }, + }; +} + export default { name: 'pins', components: { @@ -99,15 +112,7 @@ export default { noMore, }, data() { - return { - blocks: [], - blocksMap: {}, - status: { - loading: false, - hasNext: true, - offset: 0, - }, - }; + return initialData(); }, props: { pinFilters: { @@ -173,6 +178,19 @@ export default { } return true; }, + initialize() { + this.fetchMore(true); + }, + reset() { + const data = initialData(); + Object.entries(data).forEach( + (kv) => { + const [key, value] = kv; + this[key] = value; + }, + ); + this.initialize(); + }, fetchMore(created) { if (!this.shouldFetchMore(created)) { return; @@ -208,8 +226,9 @@ export default { }, }, created() { + bus.bus.$on(bus.events.refreshPin, this.reset); this.registerScrollEvent(); - this.fetchMore(true); + this.initialize(); }, }; diff --git a/pinry-spa/src/components/pin_create/PinCreateModal.vue b/pinry-spa/src/components/pin_create/PinCreateModal.vue index 11b3ae8..7886bef 100644 --- a/pinry-spa/src/components/pin_create/PinCreateModal.vue +++ b/pinry-spa/src/components/pin_create/PinCreateModal.vue @@ -75,6 +75,7 @@