Skip to content

Commit 2b458a3

Browse files
committed
Fixes FirebaseExtended#260 - $bind not working from inside loaded event
Fixes FirebaseExtended#209 - $bind will resolve after the `loaded` event has completed, fixing data load issues
1 parent 9a8b767 commit 2b458a3

File tree

3 files changed

+24
-35
lines changed

3 files changed

+24
-35
lines changed

angularfire.js

+21-32
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// as normal, except that the changes are also sent to all other clients
66
// instead of just a server.
77
//
8-
// AngularFire 0.7.1
8+
// AngularFire 0.7.2-pre
99
// http://angularfire.com
1010
// License: MIT
1111

@@ -645,7 +645,8 @@
645645
var self = this;
646646
if( self._loaded && ['child_added', 'loaded', 'value'].indexOf(evt) > -1 ) {
647647
self._timeout(function() {
648-
var parsedValue = angular.isObject(self._object)? self._parseObject(self._object) : self._object;
648+
var parsedValue = self._object.hasOwnProperty('$value')?
649+
self._object.$value : self._parseObject(self._object);
649650
switch(evt) {
650651
case 'loaded':
651652
callback(parsedValue);
@@ -711,6 +712,24 @@
711712
self._fRef.ref().update(self._parseObject(local));
712713
}
713714

715+
// When the scope is destroyed, unbind automatically.
716+
scope.$on("$destroy", function() {
717+
unbind();
718+
});
719+
720+
// Once we receive the initial value, the promise will be resolved.
721+
self._object.$on('loaded', function(value) {
722+
self._timeout(function() {
723+
if(value === null && typeof defaultFn === 'function') {
724+
scope[name] = defaultFn();
725+
}
726+
else {
727+
scope[name] = value;
728+
}
729+
deferred.resolve(unbind);
730+
});
731+
});
732+
714733
// We're responsible for setting up scope.$watch to reflect local changes
715734
// on the Firebase data.
716735
var unbind = scope.$watch(name, function() {
@@ -738,36 +757,6 @@
738757
}
739758
}, true);
740759

741-
// When the scope is destroyed, unbind automatically.
742-
scope.$on("$destroy", function() {
743-
unbind();
744-
});
745-
746-
// Once we receive the initial value, the promise will be resolved.
747-
self._fRef.once("value", function(snap) {
748-
self._timeout(function() {
749-
// HACK / FIXME: Objects require a second event loop run, since we
750-
// switch from value events to child_added. See #209 on Github.
751-
if (typeof snap.val() != "object") {
752-
// If the remote value is not set and defaultFn was provided,
753-
// initialize the local value with the result of defaultFn().
754-
if (snap.val() == null && typeof defaultFn === 'function') {
755-
scope[name] = defaultFn();
756-
}
757-
deferred.resolve(unbind);
758-
} else {
759-
self._timeout(function() {
760-
// If the remote value is not set and defaultFn was provided,
761-
// initialize the local value with the result of defaultFn().
762-
if (snap.val() == null && typeof defaultFn === 'function') {
763-
scope[name] = defaultFn();
764-
}
765-
deferred.resolve(unbind);
766-
});
767-
}
768-
});
769-
});
770-
771760
return deferred.promise;
772761
},
773762

0 commit comments

Comments
 (0)