Skip to content

Commit

Permalink
Merge pull request #338 from firebase/property-deletions
Browse files Browse the repository at this point in the history
Fix syncing of deleted properties
  • Loading branch information
tstirrat committed Dec 4, 2015
2 parents d8fdf69 + d256dea commit 63fa8b3
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 9 deletions.
19 changes: 19 additions & 0 deletions addon/serializers/firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,25 @@ import assign from 'lodash/object/assign';
export default DS.JSONSerializer.extend(DS.EmbeddedRecordsMixin, {
isNewSerializerAPI: true,

/**
* Firebase does not send null values, it omits the key altogether. This nullifies omitted
* properties so that property deletions sync correctly.
*
* @override
*/
extractAttributes: function (modelClass, resourceHash) {
var attributes = this._super(modelClass, resourceHash);

// nullify omitted attributes
modelClass.eachAttribute(function (key) {
if (!attributes.hasOwnProperty(key)) {
attributes[key] = null;
}
});

return attributes;
},

/**
* @override
*/
Expand Down
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fixed - Deleted model properties were not synchronizing.
22 changes: 15 additions & 7 deletions tests/integration/updates-from-server-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,30 @@ describe('Integration: FirebaseAdapter - Updates from server', function() {
});

describe('A record coming from find', function() {
var newPost;
var reference, newPost;

beforeEach(function(done) {
var reference = adapter._ref;
reference = adapter._ref;
Ember.run(function() {
store.findRecord('post', 'post_1').then(function(post) {
newPost = post;
reference.child('posts/post_1/body').set('Updated', function() {
done();
});
done();
});
});
});

it('receives server updates correctly', function() {
expect(newPost.get('body')).to.equal('Updated', 'property should change');
it('receives server updates correctly', function(done) {
reference.child('posts/post_1/body').set('Updated', function() {
expect(newPost.get('body')).to.equal('Updated', 'property should change');
done();
});
});

it('receives server property deletions correctly', function(done) {
reference.child('posts/post_1/body').remove(function() {
expect(newPost.get('body')).to.equal(null, 'property should be deleted');
done();
});
});

it('has the correct .ref()', function() {
Expand Down
6 changes: 4 additions & 2 deletions tests/unit/serializers/firebase-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,8 @@ describeModule(

expect(json.included).to.have.length(1);
expect(json.included[0].attributes).to.deep.equal({
firstName: 'Adam'
firstName: 'Adam',
created: null
});

expect(json.included[0].id).to.equal('aputinski');
Expand Down Expand Up @@ -486,7 +487,8 @@ describeModule(

expect(json.included).to.have.length(1);
expect(json.included[0].attributes).to.deep.equal({
firstName: 'Adam'
firstName: 'Adam',
created: null
});

expect(json.included[0].id).to.equal('aputinski');
Expand Down

0 comments on commit 63fa8b3

Please sign in to comment.