Skip to content

Commit

Permalink
Fix syncing of deleted properties
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Stirrat committed Dec 4, 2015
1 parent 091800d commit d256dea
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 d256dea

Please sign in to comment.