Skip to content
This repository was archived by the owner on May 19, 2025. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 47 additions & 10 deletions firebase-query.html
Original file line number Diff line number Diff line change
Expand Up @@ -280,12 +280,14 @@

__queryChanged: function(query, oldQuery) {
if (oldQuery) {
oldQuery.off('value', this.__onFirebaseValue, this);
oldQuery.off('child_added', this.__onFirebaseChildAdded, this);
oldQuery.off('child_removed', this.__onFirebaseChildRemoved, this);
oldQuery.off('child_changed', this.__onFirebaseChildChanged, this);
oldQuery.off('child_moved', this.__onFirebaseChildMoved, this);

this.syncToMemory(function() {
this.__map = {};
this.set('data', this.zeroValue);
});
}
Expand All @@ -299,6 +301,16 @@
}

this._onOnce = true;

/* We want the value callback to batch load the initial data,
* then let child_added take over for subsequent changes.
*/
this.__initialLoadDone = false;

/* Don't use query.once() as we need to be able to cancel
* the callback if the query changes
*/
query.on('value', this.__onFirebaseValue, this.__onError, this);
query.on('child_added', this.__onFirebaseChildAdded, this.__onError, this);
query.on('child_removed', this.__onFirebaseChildRemoved, this.__onError, this);
query.on('child_changed', this.__onFirebaseChildChanged, this.__onError, this);
Expand All @@ -317,17 +329,38 @@
return -1;
},

__onFirebaseValue: function(snapshot) {
if (snapshot.hasChildren()) {
var data = [];
snapshot.forEach(function(childSnapshot) {
var key = childSnapshot.key;
var value = this.__valueWithKey(key, childSnapshot.val())
this.__map[key] = value;
data.push(value)
}.bind(this))

this.set('data', data);
}

/* Now let child_added deal with subsequent changes */
this.query.off('value', this.__onFirebaseValue, this);
this.__initialLoadDone = true;
},

__onFirebaseChildAdded: function(snapshot, previousChildKey) {
var key = snapshot.key;
var value = snapshot.val();
var previousChildIndex = this.__indexFromKey(previousChildKey);

this._log('Firebase child_added:', key, value);
if (this.__initialLoadDone) {
var value = snapshot.val();
var previousChildIndex = this.__indexFromKey(previousChildKey);

value = this.__snapshotToValue(snapshot);
this._log('Firebase child_added:', key, value);

this.__map[key] = value;
this.splice('data', previousChildIndex + 1, 0, value);
value = this.__snapshotToValue(snapshot);

this.__map[key] = value;
this.splice('data', previousChildIndex + 1, 0, value);
}
},

__onFirebaseChildRemoved: function(snapshot) {
Expand Down Expand Up @@ -402,10 +435,7 @@
}
},

__snapshotToValue: function(snapshot) {
var key = snapshot.key;
var value = snapshot.val();

__valueWithKey: function(key, value) {
var leaf = typeof value !== 'object';

if (leaf) {
Expand All @@ -414,6 +444,13 @@
value.$key = key;
}
return value;
},

__snapshotToValue: function(snapshot) {
var key = snapshot.key;
var value = snapshot.val();

return this.__valueWithKey(key, value);
}
});
})();
Expand Down