问题
I'm following along the with the basic AngularFire2 docs, and the general format seems to be:
const items = af.database.list('/items');
// to get a key, check the Example app below
items.update('key-of-some-data', { size: newSize });
My confusion is that in the source code, it seems as though calling database.list() grabs all the data at the listed url (line 114 here)
Can anyone help clarify how that works? If it does indeed grab all the data, is there a better way of getting a reference without doing that? Or should I just reference each particular URL individually?
Thanks!
回答1:
When you create an AngularFire2 list, it holds an internal Firebase ref - accessible via the list's public $ref
property.
The list is an Observable
- which serves as the interface for reading from the database - and includes some additional methods for writing to the database: push
, update
and remove
.
In the code in your question, you are only calling the update
method and are not subscribing to the observable, so no data is loaded from the database into memory:
const items = af.database.list('/items');
// to get a key, check the Example app below
items.update('key-of-some-data', { size: newSize });
It's only when a subscription to the observable is made that listeners for value
and the child_...
events are added to the ref and the list builds and maintains an internal array that's emitted via the observable. So if you are only calling the methods that write to the database, it won't be loading any data.
The AngularFire2 object is implemented in a similar manner.
来源:https://stackoverflow.com/questions/41601241/does-angularfire2s-database-list-hold-a-reference-or-actually-grab-data