Skip to content

Instantly share code, notes, and snippets.

@bekliev
Forked from stwilz/curryMapGetters.js
Last active December 11, 2020 10:07
Show Gist options
  • Save bekliev/5fbd348054d2f57bc0b0af0720b9cfff to your computer and use it in GitHub Desktop.
Save bekliev/5fbd348054d2f57bc0b0af0720b9cfff to your computer and use it in GitHub Desktop.
A curry utility for passing additional arguments to `mapGetters`.
import { mapGetters } from 'vuex';
const curryMapGetters = (...args) => (namespace, getters) => {
const getters = mapGetters(namespace, getters);
const entries = Object.entries(getters);
const initialAccumulator = {};
return entries.reduce((acc, [getterName, getterHandler]) => {
acc[getterName] = (state) => {
return getterHandler.call(state)(...args);
};
return acc;
}, initialAccumulator);
};
const curryMapActions = (...args) => (namespace, actions) => {
// Currently in development
};
<template>
<section class="media-list">
<media-table
:rows="list.list"
:sord="list.order.sord"
:sidx="list.order.sidx"
:page="list.pagination.page"
:perPage="list.pagination.perPage"
:total="list.total"
@sort-change="listSortChange"
@page-change="listPaginationChange"
@size-change="listPaginationChange"
/>
</section>
</template>
<script>
const LISTS_TYPE = {
MEDIA: 'media',
CONTENT: 'content',
};
export default {
name: 'example',
computed: {
...curryMapGetters(LISTS_TYPE.MEDIA)('Lists', [
'list',
]),
},
created() {
// NOTE: currently in development
this.listInitialize(); // auto-magically passes LISTS_TYPE.MEDIA as first argument
},
methods: {
// NOTE: currently in development
...curryMapActions(LISTS_TYPE.MEDIA)('Lists', [
'listInitialize',
'listSearch',
'listSortChange',
'listPaginationChange',
]),
},
};
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment