A simple attempt to help using jsdata alongside Vue.js: This plugin syncing Vuex store with js-data After each injection made by js-data some silent mutations are triggered to ensure vuex store is kept in sync with your ressources (and trigger reactivity).
Read more : js-data/js-data#57
This plugin is developed for
You're welcome to contribute to help compatibility issues.
With NPM
npm install vuex-plugin-jsdata
Then when you setup vuex:
import jsdataPlugin from 'vuex-plugin-jsdata'
import yourJsDataStore from 'xxxx'
const plugins = [
jsdataPlugin(yourJsDataStore),
... // other plugins
]
new Vuex.Store({
// state,
// actions,
// mutations,
plugins,
})
Every change in a js-data ressource are made with the DSInject method. The plugin manage the state tree(vuex) under a DS module by listening to the afterInject hook (js-data)
vuex-plugin-jsdata fire only one silent mutation :
REFRESH_DATASTORE
Although all local ressources injected in the jsdata-store can be found in the vuex store under the namespaced module DS, the plugin provide automatic getters for every model.
Ex:
// Register a model in js-data
export const User = store.defineResource({
name: 'user',
endpoint: 'users',
})
// in a .vue component
<script>
import { mapGetters } from 'vuex'
export default {
name: 'User',
props: {
id: { required: true },
},
computed: {
...mapGetters(['DSUser']),
user() {
return this.DSUser[this.id]
},
},
}
</script>
<template lang="jade">
div.user
pre {{ user | json }}
</template>
This plugin provide a handy way to make a ressource available inside components.
mapRessources([ { nameOfTheGetter: [nameOfTheRessource:string, id_key:string]}, ... ]) mapRessources is a getter factory designed to get a single record which id is computed from $vm[id_key]. Its really useful for getting specific records dynamicly (eg: get user with id picked from router params) example:
// in store - DSUsers: { 1: { name: 'Alex' } }
// component definition
// using the object spread operator
$vm = {
data() {
return {
user_id: 1
}
},
computed: {
...mapRessources([
{ user: ['User', 'user_id'] }
{ userFromRoute: ['User', '$route.params.id'] } // with vue-router
]),
}
}
// Log
$vm.user.name -> 'Alex'
$vm.userFromRoute.name -> 'Alex'
Clone the repo and run
npm install
npm run example-simple
=> go to /examples/simple
more to come ...
[ ] handle config options [ ] some examples
are welcome :)