Feature: Add global event bus to trigger pin-refresh

+ Any other good idea to do this?
This commit is contained in:
winkidney
2019-12-05 00:00:16 +08:00
committed by Isaac Bythewood
parent 0006aad6f7
commit b3a065a4fb
3 changed files with 41 additions and 10 deletions

View File

@@ -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();
},
};
</script>

View File

@@ -75,6 +75,7 @@
<script>
import API from '../api';
import FileUpload from './FileUpload.vue';
import bus from '../utils/bus';
function isURLBlank(url) {
return url !== null && url !== '';
@@ -131,6 +132,7 @@ export default {
}
promise.then(
(resp) => {
bus.bus.$emit(bus.events.refreshPin);
self.$emit('pinCreated', resp);
self.$parent.close();
},

View File

@@ -0,0 +1,10 @@
import Vue from 'vue';
const eventBus = new Vue();
export default {
bus: eventBus,
events: {
refreshPin: 'refreshPin',
},
};