Skip to content
This repository was archived by the owner on Mar 17, 2025. It is now read-only.

Commit 75da6a0

Browse files
soumak77Jacob Wenger
authored andcommitted
Add $resolved property to get $loaded() state (#837)
1 parent 3785497 commit 75da6a0

File tree

4 files changed

+84
-0
lines changed

4 files changed

+84
-0
lines changed

src/database/FirebaseArray.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,14 @@
8888

8989
this._sync.init(this.$list);
9090

91+
// $resolved provides quick access to the current state of the $loaded() promise.
92+
// This is useful in data-binding when needing to delay the rendering or visibilty
93+
// of the data until is has been loaded from firebase.
94+
this.$list.$resolved = false;
95+
this.$loaded().finally(function() {
96+
self.$list.$resolved = true;
97+
});
98+
9199
return this.$list;
92100
}
93101

src/database/FirebaseObject.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
if( !(this instanceof FirebaseObject) ) {
3737
return new FirebaseObject(ref);
3838
}
39+
var self = this;
3940
// These are private config props and functions used internally
4041
// they are collected here to reduce clutter in console.log and forEach
4142
this.$$conf = {
@@ -63,6 +64,14 @@
6364

6465
// start synchronizing data with Firebase
6566
this.$$conf.sync.init();
67+
68+
// $resolved provides quick access to the current state of the $loaded() promise.
69+
// This is useful in data-binding when needing to delay the rendering or visibilty
70+
// of the data until is has been loaded from firebase.
71+
this.$resolved = false;
72+
this.$loaded().finally(function() {
73+
self.$resolved = true;
74+
});
6675
}
6776

6877
FirebaseObject.prototype = {

tests/unit/FirebaseArray.spec.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,39 @@ describe('$firebaseArray', function () {
590590
});
591591
});
592592

593+
describe('$resolved', function () {
594+
it('should return false on init', function () {
595+
arr = $firebaseArray(stubRef());
596+
expect(arr.$resolved).toBe(false);
597+
});
598+
599+
it('should return true once $loaded() promise is resolved', function () {
600+
arr = $firebaseArray(stubRef());
601+
602+
arr.$loaded()
603+
.finally(function () {
604+
expect(arr.$resolved).toBe(true);
605+
done();
606+
});
607+
});
608+
609+
it('should return true once $loaded() promise is rejected', function () {
610+
var err = new Error('test_fail');
611+
612+
spyOn(firebase.database.Reference.prototype, "once").and.callFake(function (event, cb, cancel_cb) {
613+
cancel_cb(err);
614+
});
615+
616+
arr = $firebaseArray(stubRef());
617+
618+
arr.$loaded()
619+
.finally(function () {
620+
expect(arr.$resolved).toBe(true);
621+
done();
622+
});
623+
});
624+
});
625+
593626
describe('$ref', function() {
594627
it('should return Firebase instance it was created with', function() {
595628
var ref = stubRef();

tests/unit/FirebaseObject.spec.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,40 @@ describe('$firebaseObject', function() {
240240
});
241241
});
242242

243+
describe('$resolved', function () {
244+
it('should return false on init', function () {
245+
var ref = stubRef();
246+
var obj = $firebaseObject(ref);
247+
expect(obj.$resolved).toBe(false);
248+
});
249+
250+
it('should return true once $loaded() promise is resolved', function () {
251+
var obj = makeObject();
252+
253+
obj.$loaded()
254+
.finally(function () {
255+
expect(obj.$resolved).toBe(true);
256+
done();
257+
});
258+
});
259+
260+
it('should return true once $loaded() promise is rejected', function () {
261+
var err = new Error('test_fail');
262+
263+
spyOn(firebase.database.Reference.prototype, "once").and.callFake(function (event, cb, cancel_cb) {
264+
cancel_cb(err);
265+
});
266+
267+
var obj = makeObject();
268+
269+
obj.$loaded()
270+
.finally(function () {
271+
expect(obj.$resolved).toBe(true);
272+
done();
273+
});
274+
});
275+
});
276+
243277
describe('$ref', function () {
244278
it('should return the Firebase instance that created it', function () {
245279
var ref = stubRef();

0 commit comments

Comments
 (0)