Skip to content

Commit

Permalink
Merge 2e0b0cc into 83edb53
Browse files Browse the repository at this point in the history
  • Loading branch information
jamestalmage committed Jan 30, 2015
2 parents 83edb53 + 2e0b0cc commit 186388c
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 11 deletions.
12 changes: 10 additions & 2 deletions src/FirebaseObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,16 @@
}

var send = $firebaseUtils.debounce(function() {
rec.$$scopeUpdated(getScope())
['finally'](function() { sending = false; });
var scopeData = getScope();
rec.$$scopeUpdated(scopeData)
['finally'](function() {
sending = false;
if(!scopeData.hasOwnProperty('$value')){
delete rec.$value;
delete parsed(scope).$value;
}
}
);
}, 50, 500);

var scopeUpdated = function() {
Expand Down
16 changes: 7 additions & 9 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,22 +301,20 @@
});
},

extendData: function(dest, source) {
utils.each(source, function(v,k) {
dest[k] = utils.deepCopy(v);
});
return dest;
},

scopeData: function(dataOrRec) {
var data = {
$id: dataOrRec.$id,
$priority: dataOrRec.$priority
};
if( dataOrRec.hasOwnProperty('$value') ) {
var hasPublicProp = false;
utils.each(dataOrRec, function(v,k) {
hasPublicProp = true;
data[k] = utils.deepCopy(v);
});
if(!hasPublicProp && dataOrRec.hasOwnProperty('$value')){
data.$value = dataOrRec.$value;
}
return utils.extendData(data, dataOrRec);
return data;
},

updateRec: function(rec, snap) {
Expand Down
15 changes: 15 additions & 0 deletions tests/unit/FirebaseObject.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,21 @@ describe('$FirebaseObject', function() {
expect($scope.test).toEqual({$value: null, $id: obj.$id, $priority: obj.$priority});
});

it('should delete $value if set to an object', function () {
var $scope = $rootScope.$new();
var obj = makeObject();
obj.$bindTo($scope, 'test');
obj.$$$ready(null);
expect($scope.test).toEqual({$value: null, $id: obj.$id, $priority: obj.$priority});
$scope.$apply(function() {
$scope.test.text = 'hello';
});
$interval.flush(500);
$timeout.flush(); // for $interval
//$timeout.flush(); // for $watch
expect($scope.test).toEqual({text: 'hello', $id: obj.$id, $priority: obj.$priority});
});

it('should update $priority if $priority changed in $scope', function () {
var $scope = $rootScope.$new();
var spy = obj.$inst().$set;
Expand Down
23 changes: 23 additions & 0 deletions tests/unit/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,29 @@ describe('$firebaseUtils', function () {
$utils.updateRec(rec, testutils.snap({bar: 'baz', baz: 'foo'}));
expect(rec).toEqual({bar: 'baz', baz: 'foo', $id: 'foo', $priority: null})
});

it('should delete $value property if not a primitive',function(){
var rec = {$id:'foo', $priority:null, $value:null };
$utils.updateRec(rec, testutils.snap({bar: 'baz', baz:'foo'}));
expect(rec).toEqual({bar: 'baz', baz: 'foo', $id: 'foo', $priority: null});
});
});

describe('#scopeData',function(){
it('$id, $priority, and $value are only private properties that get copied',function(){
var data = {$id:'foo',$priority:'bar',$value:null,$private1:'baz',$private2:'foo'};
expect($utils.scopeData(data)).toEqual({$id:'foo',$priority:'bar',$value:null});
});

it('all public properties will be copied',function(){
var data = {$id:'foo',$priority:'bar',public1:'baz',public2:'test'};
expect($utils.scopeData(data)).toEqual({$id:'foo',$priority:'bar',public1:'baz',public2:'test'});
});

it('$value will not be copied if public properties are present',function(){
var data = {$id:'foo',$priority:'bar',$value:'noCopy',public1:'baz',public2:'test'};
expect($utils.scopeData(data)).toEqual({$id:'foo',$priority:'bar',public1:'baz',public2:'test'});
});
});

describe('#applyDefaults', function() {
Expand Down

0 comments on commit 186388c

Please sign in to comment.