angularfire icon indicating copy to clipboard operation
angularfire copied to clipboard

$bindTo removes methods defined via $FirebaseObject.$extendFactory

Open gjungb opened this issue 12 years ago • 2 comments

After defining methods via $FirebaseObject.$extendFactory these can be used in the view. However, if the object is bound to a scope variable via $bindTo it is no longer an instance of ChildClassbut of Objectand these methods can not be executed anymore.

In the following pen http://codepen.io/gjungb/pen/FkEsi the temperature in °C is not displayed, if weather.$bindTo($scope, 'weather'); is used.

I think this is a bug.

gjungb avatar Aug 05 '14 15:08 gjungb

$bindTo binds a copy of the synchronized data onto the scope. Since methods added using $extendFactory are on the prototype, they will not be added into the bound scope variable.

Understanding how $watch works and the internals of how object references work make it more obvious why this is necessary. If we simply bound the synchronized object to the scope, then each time an update occurred, both the origin object and the bound scope data (sharing the same pointer) would be simultaneously updated and the watch methods would have nothing to compare to determine equality, and thus wouldn't be able to determine if a change occurred.

The primary purpose of $bindTo is to create quick 3-way data bindings for simple data manipulation. If you want advanced capabilities, skip $bindTo and just assign your sync'd object right to the scope:

$scope.data = $firebase(ref).$asObject();

Then call $save when you want to perform updates.

katowulf avatar Aug 05 '14 15:08 katowulf

Thanks! Got it!

gjungb avatar Aug 05 '14 15:08 gjungb