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

Commit 57ef116

Browse files
committed
Merge pull request #541 from jamestalmage/initial-null-value-bug
Fix primitive to object transitions.
2 parents c485405 + 91bebbf commit 57ef116

File tree

4 files changed

+55
-11
lines changed

4 files changed

+55
-11
lines changed

src/FirebaseObject.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,16 @@
329329
}
330330

331331
var send = $firebaseUtils.debounce(function(val) {
332-
rec.$$scopeUpdated($firebaseUtils.scopeData(val))
333-
['finally'](function() { sending = false; });
332+
var scopeData = $firebaseUtils.scopeData(val);
333+
rec.$$scopeUpdated(scopeData)
334+
['finally'](function() {
335+
sending = false;
336+
if(!scopeData.hasOwnProperty('$value')){
337+
delete rec.$value;
338+
delete parsed(scope).$value;
339+
}
340+
}
341+
);
334342
}, 50, 500);
335343

336344
var scopeUpdated = function(newVal) {

src/utils.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -301,22 +301,20 @@
301301
});
302302
},
303303

304-
extendData: function(dest, source) {
305-
utils.each(source, function(v,k) {
306-
dest[k] = utils.deepCopy(v);
307-
});
308-
return dest;
309-
},
310-
311304
scopeData: function(dataOrRec) {
312305
var data = {
313306
$id: dataOrRec.$id,
314307
$priority: dataOrRec.$priority
315308
};
316-
if( dataOrRec.hasOwnProperty('$value') ) {
309+
var hasPublicProp = false;
310+
utils.each(dataOrRec, function(v,k) {
311+
hasPublicProp = true;
312+
data[k] = utils.deepCopy(v);
313+
});
314+
if(!hasPublicProp && dataOrRec.hasOwnProperty('$value')){
317315
data.$value = dataOrRec.$value;
318316
}
319-
return utils.extendData(data, dataOrRec);
317+
return data;
320318
},
321319

322320
updateRec: function(rec, snap) {

tests/unit/FirebaseObject.spec.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,21 @@ describe('$FirebaseObject', function() {
322322
expect($scope.test).toEqual({$value: null, $id: obj.$id, $priority: obj.$priority});
323323
});
324324

325+
it('should delete $value if set to an object', function () {
326+
var $scope = $rootScope.$new();
327+
var obj = makeObject();
328+
obj.$bindTo($scope, 'test');
329+
obj.$$$ready(null);
330+
expect($scope.test).toEqual({$value: null, $id: obj.$id, $priority: obj.$priority});
331+
$scope.$apply(function() {
332+
$scope.test.text = 'hello';
333+
});
334+
$interval.flush(500);
335+
$timeout.flush(); // for $interval
336+
//$timeout.flush(); // for $watch
337+
expect($scope.test).toEqual({text: 'hello', $id: obj.$id, $priority: obj.$priority});
338+
});
339+
325340
it('should update $priority if $priority changed in $scope', function () {
326341
var $scope = $rootScope.$new();
327342
var spy = obj.$inst().$set;

tests/unit/utils.spec.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,29 @@ describe('$firebaseUtils', function () {
102102
$utils.updateRec(rec, testutils.snap({bar: 'baz', baz: 'foo'}));
103103
expect(rec).toEqual({bar: 'baz', baz: 'foo', $id: 'foo', $priority: null})
104104
});
105+
106+
it('should delete $value property if not a primitive',function(){
107+
var rec = {$id:'foo', $priority:null, $value:null };
108+
$utils.updateRec(rec, testutils.snap({bar: 'baz', baz:'foo'}));
109+
expect(rec).toEqual({bar: 'baz', baz: 'foo', $id: 'foo', $priority: null});
110+
});
111+
});
112+
113+
describe('#scopeData',function(){
114+
it('$id, $priority, and $value are only private properties that get copied',function(){
115+
var data = {$id:'foo',$priority:'bar',$value:null,$private1:'baz',$private2:'foo'};
116+
expect($utils.scopeData(data)).toEqual({$id:'foo',$priority:'bar',$value:null});
117+
});
118+
119+
it('all public properties will be copied',function(){
120+
var data = {$id:'foo',$priority:'bar',public1:'baz',public2:'test'};
121+
expect($utils.scopeData(data)).toEqual({$id:'foo',$priority:'bar',public1:'baz',public2:'test'});
122+
});
123+
124+
it('$value will not be copied if public properties are present',function(){
125+
var data = {$id:'foo',$priority:'bar',$value:'noCopy',public1:'baz',public2:'test'};
126+
expect($utils.scopeData(data)).toEqual({$id:'foo',$priority:'bar',public1:'baz',public2:'test'});
127+
});
105128
});
106129

107130
describe('#applyDefaults', function() {

0 commit comments

Comments
 (0)