Package Exports
- vuex-loopback
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (vuex-loopback) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Vuex module factory for Loopback
Installing
1. Install axios and vuex-loopback.
yarn add axios vuex-loopbackor
npm install axios vuex-loopback2. Import axios and module factory.
import axios from 'axios';
import {createModule} from 'vuex-loopback';3. Create axios instance with baseURL option.
const client = axios.create({
baseURL: 'https://my-domain.com/api/',
});Create Vuex module
1. Define collection model with default fields.
const model = {
id: '',
name: '',
}2. Use module factory to create Vuex module.
new Vuex.Store({
modules: {
// ...
articles: {
namespaced: true,
...createModule({
client, // (required) Axios instance.
model, // (required) Collection model.
collection: 'Articles', // (required) Plural collection name.
state: {}, // Extend default state.
getters: {}, // Extend default getters.
actions: {}, // Extend default actions.
mutations: {}, // Extend default mutations.
}),
},
// ...
}
});Load items by ItemsLoader
1. Import from vuex-loopback.
import {ItemsLoader} from 'vuex-loopback';2. Define local component.
export default {
// ...
components: {
ItemsLoader,
},
// ...
}3. Use it to load collection items.
<!-- Loader -->
<items-loader
module="articles">
<template
slot-scope="loader">
<!-- Item -->
<div
:key="item.id"
v-for="item in loader.items">
{{ item.name }}
</div>
<!-- More Button -->
<button
v-if="loader.hasMore"
@click="loader.loadMore">
More
</button>
</template>
</items-loader>Create, edit or remove an item by ItemEditor
1. Import from vuex-loopback.
import {ItemEditor} from 'vuex-loopback'; // new line
import {ItemsLoader} from 'vuex-loopback';2. Define local component.
export default {
// ...
components: {
ItemEditor, // new line
ItemsLoader,
},
// ...
}3. Use it to manage collection item.
<!-- Loader -->
<items-loader
module="articles">
<template
slot-scope="loader">
<!-- Item -->
<div
:key="item.id"
v-for="item in loader.items">
{{ item.name }}
<!-- Edit Button -->
<button
@click="() => $refs.editor.show(item)">
Edit
</button>
</div>
<!-- More Button -->
<button
v-if="loader.hasMore"
@click="loader.loadMore">
More
</button>
<!-- Create Button -->
<button
@click="() => $refs.editor.show()">
Create
</button>
</template>
</items-loader>
<!-- Editor -->
<item-editor
ref="editor"
module="articles">
<template
slot-scope="editor">
<form
@submit.prevent="editor.save">
<!-- Name Field -->
<input
:value="editor.item.name"
@input="editor.set({...editor.item, name: $event})"/>
<!-- Save Button -->
<button
type="submit">
Save
</button>
<!-- Remove Button -->
<button
v-if="editor.item.id"
@click="editor.remove">
Remove
</button>
</form>
</template>
</item-editor>What's next
Sometimes you may want to use Vuex Module directly.
Let's see what it has.
State
- item -
null - tempItem -
null - items -
[] - skip -
0 - limit -
20 - total -
0 - orderBy -
'' - orderDesc -
false - searchBy -
['name'] - searchQuery -
'' - where -
{} - loading -
false - include -
[] - fields -
[]
Getters
page- Number of current page.totalPages- Number of total pages.hasMore- Can I load more? (lazy loading)
Mutations
- RESET
- SET_ITEM (
value) - RESET_ITEM
- SET_TEMP_ITEM (
value) - RESET_TEMP_ITEM
- SET_ITEMS (
value) - RESET_ITEMS
- SET_SKIP (
value) - RESET_SKIP
- SET_LIMIT (
value) - RESET_LIMIT
- SET_TOTAL (
value) - RESET_TOTAL
- SET_ORDER_BY (
value) - RESET_ORDER_BY
- SET_ORDER_DESC (
value) - RESET_ORDER_DESC
- SET_SEARCH_BY (
value) - RESET_SEARCH_BY
- SET_SEARCH_QUERY (
value) - RESET_SEARCH_QUERY
- SET_WHERE (
value) - RESET_WHERE
- SET_LOADING (
value) - RESET_LOADING
- SET_INCLUDE (
value) - RESET_INCLUDE
- SET_FIELDS (
value) - RESET_FIELDS
- UPDATE_ITEM (
item) - REMOVE_ITEM (
id)
Actions:
FETCH_ITEM (
{id, filter = {}, noTempItem = false})
affect:loadingitemtempItem
FETCH_ITEMS (
{filter = {}, noGlobals = false, append = false})
affect:loadingitemstotal
CREATE_ITEM (
{data, filter = {}})
affect:loadingitems
PATCH_ITEM (
{id, data, filter = {}})
affect:loadingitemitems
REMOVE_ITEM (
id)
affect:loadingitemitems
CREATE_TEMP_ITEM (
{model})
affect:tempItem
PUT_TEMP_ITEM (
{filter = {}, existed = false, reset = false})
affect:loadingitemitemstempItem
SEARCH_ITEMS (
{query = '', searchBy = null})
affect:loadingitemitemstotal
FETCH_PAGE (
{page = 1})
affect:loadingitemstotal
FETCH_MORE
affect:loadingitems
Todo
- State factory.
- Mutations factory.
- Actions factory.
- Getters factory.
- Documentation.