Open
Description
If you add res['.ref'] = _getRef(snapshot)
to the createRecord function. Like:
function createRecord (snapshot) {
var value = snapshot.val()
var res = isObject(value)
? value
: { '.value': value }
res['.key'] = _getKey(snapshot)
res['.ref'] = _getRef(snapshot);
return res
}
Then we can more easily access the firebase reference that is associated with a given item. The example in the readme could be changed from:
deleteItem: function (item) {
this.$firebaseRefs.items.child(item['.key']).remove()
}
to:
deleteItem: function (item) {
item['.ref'].remove()
}
Activity
posva commentedon Apr 21, 2018
mmh, this is interesting. Any situation when
$firebaseRefs
is not enough?anachirino commentedon Apr 21, 2018
It's just easier access. Make the resulting user code much more readable IMHO.
posva commentedon Apr 21, 2018
oh wait, you're asking to add a ref for every single element? That is too much. I will have to think about it. I like how practical it is but it introduces other problems
anachirino commentedon Apr 21, 2018
would making it a function like:
res['.ref'] = function() { return _getRef(snapshot); }
Help your concerns?
anachirino commentedon Apr 28, 2018
@posva any updates?
posva commentedon Apr 29, 2018
not yet, I'll look into this later
trickstival commentedon Oct 7, 2018
@anachirino or it could be a getter with Object.defineProperty
posva commentedon Jul 16, 2019
For the moment, this is now achievable with the serialize option (https://vuefire.vuejs.org/api/vuefire.html#options-serialize)
chrisspiegl commentedon Dec 31, 2022
I was able to achieve the result by doing this in my
main.js
:note the line:
data.ref = snapshot.ref
Sadly, it does not do the same for the referenced children which are loaded. Those end up not having a ref.
UPDATE: I later noticed that this also get's pushed to the firestore upon update. So it's not read only and I don't know how I would make it read only 🙈.
posva commentedon Dec 31, 2022
Use Object.defineProperty to make it non enumerable 😉
BenJackGill commentedon Mar 29, 2023
How does this work with TypeScript?
I have tried this without any luck.
The code works, because I can access
ref
on my useDocument objects. But TypeScript still complains with an error.For example, when I try to access
ref
on aUser
document I get this TypeScript error:Property 'ref' does not exist on type 'User & { readonly id: string; }'. ts(2339)
BenJackGill commentedon Mar 30, 2023
Also I have noticed this does not work for
useCollection()
. It only works foruseDocument()
.When using
useCollection()
it would be good to have a collection reference on the collection itself, and a document reference stored on aref
property of on each document in the collection array.2 remaining items