t2t2 / vue-syncers-feathers

Synchronises feathers services with vue objects, updated in real time
MIT License
32 stars 2 forks source link
feathersjs vue

vue-syncers-feathers

Synchronises feathers services with vue objects, updated in real-time

Build Status Coverage Status Scrutinizer Code Quality

Changelog on GitHub releases

Setup

npm install vue-syncers-feathers feathers-commons feathers-query-filters --save

Webpack/Browserify

// Set up feathers client
// You can do this whatever way you prefer, eg. feathers-client
import feathers from 'feathers/client'
import feathersIO from 'feathers-socketio/client'
import io from 'socket.io-client'
const socket = io()
const client = feathers().configure(feathersIO(socket))

// Set up vue & VueSyncersFeathers
import Vue from 'vue'
import VueSyncersFeathers from 'vue-syncers-feathers'

Vue.use(VueSyncersFeathers, {
    feathers: client
})

Configuration

ADVANCED - Most of the time you do not need these

Usage

<template>
    <div class="user-list">
        <div v-for="user in userList">
            {{user | json}}
        </div>
    </div>
</template>
<script>
export default {
    sync: {
        // put all results in users service on userList
        userList: 'users',
        // put a user with id 1 on userObject
        userObject: {
            service: 'users',
            id() {
                return 1
            }
        },
        // put filtered users list on specialUsers
        specialUsers: {
            service: 'users',
            query() {
                return {
                    // All users where user.special === true
                    special: true
                }
            }
        }
    }
}
</script>

sync option object

key: path where the object will be (vm.key)
value: string|object Service to use, or options object:

General syncer settings

To use loaded and error event handler on all syncers check instance events

Collection options (default)

vm.key will be object where keys are object IDs (empty if none matches/all deleted)

Single item options (if id is set)

vm.key will be the object which ID matches (or null on error/deletion)

Reactivity

Both id and query are sent to vm.$watch to get and observe the value. If the value is changed (eg. id: () => { return this.shownUserId } and this.shownUserId = 3 later), the new object is requested from the server. If new the value is null, the request won't be sent and current value is set to empty object (collection mode) or null (single item mode)

export default {
    data() {
        return {
            userId: 1
        }
    },
    sync: {
        user: {
            service: 'users',
            id() {
                return this.userId
            }
        }
    }
}

instance.userId = 2 // loads user id = 2

Instance methods

Instance properties

Instance events

Aliases

For cleaner code you can enable the following aliases by setting aliases option true in the Vue.use call. Note that these aren't enabled by default to avoid conflicts with any other vue plugins you might be using.

Alias Is same as Key for individual enabling
vm.$loading vm.$loadingSyncers loading
vm.$refresh() vm.$refreshSyncers refresh
vm.$service(name) vm.$feathers.service(name) service
// Enable all
Vue.use(VueSyncersFeathers, {
    aliases: true,
    feathers: client
})
// Enable some
Vue.use(VueSyncersFeathers, {
    aliases: {
        loading: true,
        service: true
    },
    feathers: client
})

Example component with aliases:

<template>
    <div>
        <div v-if="$loading">Loading...</div>
        [...]
    </div>
</template>
<script>
export default {
    methods: {
        addNewUser(user) {
            this.$service('users').create(user).then(() => {
                //...
            })
        }
    },
    sync: {
        users: 'users',
    }
}
</script>

FAQ

Compatibility warnings: