-
-
Notifications
You must be signed in to change notification settings - Fork 333
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add VueJS Composition API support to Vuefire #646
Comments
I don't have plans right now to add it but it would make sense for Vue 3. Good news is, as you found out, the core is ready for this change because it binds data to the property of an object. Which means, it could easily work by receiving a |
@torfeld6 are |
the project has changed quite a bit since I last posted March 22. I can recommend having a look at the guten-abend project. The functions I created for the RTDB: useRef.js import firebase from 'firebase/app'
import 'firebase/database'
import {computed, reactive, toRefs} from '@vue/composition-api'
export default function useRef() {
const database = firebase.database()
const state = reactive({
loading: true,
exists: false,
val: null,
docs: computed(() => {
if (state.val) {
const res = []
for (const key in state.val) {
if (state.val.hasOwnProperty(key)) {
res.push({
...state.val[key],
key
})
}
}
return res
} else {
return []
}
}),
path: null
})
async function loadByPath(path, filter = {}) {
let ref = database.ref(path)
for (const key in filter) {
ref = ref.orderByChild(key).equalTo(filter[key])
}
const snapshot = await ref.once('value')
state.exists = snapshot.exists()
state.loading = false
state.path = path
state.val = snapshot.val()
return state.exists
}
return {
...toRefs(state),
loadByPath
}
} useLiveRef.js import firebase from 'firebase/app'
import 'firebase/database'
import {computed, reactive, toRefs} from '@vue/composition-api'
import Vue from "vue";
const state = reactive({})
export default function useLiveRef(path, filter = {}) {
if (!state[path]) {
const database = firebase.database()
let ref = database.ref(path)
for (const key in filter) {
ref = ref.orderByChild(key).equalTo(filter[key])
}
Vue.set(state, path, {})
ref.on('child_added', data => {
Vue.set(state[path], data.key, data.val())
});
ref.on('child_changed', data => {
Vue.set(state[path], data.key, data.val())
});
ref.on('child_removed', data => {
Vue.delete(state[path], data.key)
});
}
const docs = computed(() => {
if (!state[path]) {
return []
}
return Object.keys(state[path]).map((key) => ({
...state[path][key],
id: key
}))
})
return {
...toRefs(state[path]),
docs
}
} I prefer guten-abend's syntax. You can use the documents directly in the component or wrap them in another function (i.e. I went back to just having a simple Kind Regards |
@torfeld6 Thanks so much for sharing all of this - it's definitely an easier starting point than the track I've been wandering down trying to get this working! |
The composition API is on the way 🚢 #1241 |
I really liked the simplicity of using
this.$bind
andthis.$rtdbBind
this project provides. Recently, I started to use the Composition API for my projects and unfortunately, these functions do not make much sense since there is no singlethis.data
reactive to bind firebase references to anymore.Are you planning to add some sort of Composition API support to Vuefire anytime soon?
In the meantime, I came up with a workaround using the functions provided by
vuefire-core
libary and created some sort of Composition API wrapper for them. I hope it can be useful to some of you. I tried to find an existing solution, but what I found did not support both database types and made a distinction between binding documents and collections (Link).use-firestore.js
use-rtdb.js
use-meeting-store.js
Splashscreen.vue
The text was updated successfully, but these errors were encountered: