Skip to content

Commit

Permalink
Add $resolved property to get $loaded() state (#837)
Browse files Browse the repository at this point in the history
  • Loading branch information
soumak77 authored and Jacob Wenger committed Aug 30, 2016
1 parent 3785497 commit 75da6a0
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/database/FirebaseArray.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@

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

// $resolved provides quick access to the current state of the $loaded() promise.
// This is useful in data-binding when needing to delay the rendering or visibilty
// of the data until is has been loaded from firebase.
this.$list.$resolved = false;
this.$loaded().finally(function() {
self.$list.$resolved = true;
});

return this.$list;
}

Expand Down
9 changes: 9 additions & 0 deletions src/database/FirebaseObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
if( !(this instanceof FirebaseObject) ) {
return new FirebaseObject(ref);
}
var self = this;
// These are private config props and functions used internally
// they are collected here to reduce clutter in console.log and forEach
this.$$conf = {
Expand Down Expand Up @@ -63,6 +64,14 @@

// start synchronizing data with Firebase
this.$$conf.sync.init();

// $resolved provides quick access to the current state of the $loaded() promise.
// This is useful in data-binding when needing to delay the rendering or visibilty
// of the data until is has been loaded from firebase.
this.$resolved = false;
this.$loaded().finally(function() {
self.$resolved = true;
});
}

FirebaseObject.prototype = {
Expand Down
33 changes: 33 additions & 0 deletions tests/unit/FirebaseArray.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,39 @@ describe('$firebaseArray', function () {
});
});

describe('$resolved', function () {
it('should return false on init', function () {
arr = $firebaseArray(stubRef());
expect(arr.$resolved).toBe(false);
});

it('should return true once $loaded() promise is resolved', function () {
arr = $firebaseArray(stubRef());

arr.$loaded()
.finally(function () {
expect(arr.$resolved).toBe(true);
done();
});
});

it('should return true once $loaded() promise is rejected', function () {
var err = new Error('test_fail');

spyOn(firebase.database.Reference.prototype, "once").and.callFake(function (event, cb, cancel_cb) {
cancel_cb(err);
});

arr = $firebaseArray(stubRef());

arr.$loaded()
.finally(function () {
expect(arr.$resolved).toBe(true);
done();
});
});
});

describe('$ref', function() {
it('should return Firebase instance it was created with', function() {
var ref = stubRef();
Expand Down
34 changes: 34 additions & 0 deletions tests/unit/FirebaseObject.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,40 @@ describe('$firebaseObject', function() {
});
});

describe('$resolved', function () {
it('should return false on init', function () {
var ref = stubRef();
var obj = $firebaseObject(ref);
expect(obj.$resolved).toBe(false);
});

it('should return true once $loaded() promise is resolved', function () {
var obj = makeObject();

obj.$loaded()
.finally(function () {
expect(obj.$resolved).toBe(true);
done();
});
});

it('should return true once $loaded() promise is rejected', function () {
var err = new Error('test_fail');

spyOn(firebase.database.Reference.prototype, "once").and.callFake(function (event, cb, cancel_cb) {
cancel_cb(err);
});

var obj = makeObject();

obj.$loaded()
.finally(function () {
expect(obj.$resolved).toBe(true);
done();
});
});
});

describe('$ref', function () {
it('should return the Firebase instance that created it', function () {
var ref = stubRef();
Expand Down

0 comments on commit 75da6a0

Please sign in to comment.