From 8630e5524ffa112055713ba98a39ff69a5507d4c Mon Sep 17 00:00:00 2001 From: Buck Doyle Date: Sun, 31 Jan 2016 16:56:32 -0500 Subject: [PATCH] Add override so serialiser saves hasMany As suggested by @fsmanuel: https://github.com/nolanlawson/ember-pouch/issues/16#issuecomment-122651303 --- addon/serializers/pouch.js | 18 ++++++++++- tests/dummy/app/models/food-item.js | 3 +- tests/dummy/app/serializers/application.js | 3 ++ .../integration/adapters/pouch-basics-test.js | 30 +++++++++++++++++++ 4 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 tests/dummy/app/serializers/application.js diff --git a/addon/serializers/pouch.js b/addon/serializers/pouch.js index 6a892ae..ec5fdad 100644 --- a/addon/serializers/pouch.js +++ b/addon/serializers/pouch.js @@ -1,3 +1,19 @@ import DS from 'ember-data'; -export default DS.RESTSerializer.extend({}); \ No newline at end of file +export default DS.RESTSerializer.extend({ + _shouldSerializeHasMany: function() { + return true; + }, + + // This fixes a failure in Ember Data 1.13 where an empty hasMany + // was saving as undefined rather than []. + serializeHasMany(snapshot, json, relationship) { + this._super.apply(this, arguments); + + const key = relationship.key; + + if (!json[key]) { + json[key] = []; + } + } +}); diff --git a/tests/dummy/app/models/food-item.js b/tests/dummy/app/models/food-item.js index 5a441cb..702c0d3 100644 --- a/tests/dummy/app/models/food-item.js +++ b/tests/dummy/app/models/food-item.js @@ -5,5 +5,6 @@ import DS from 'ember-data'; export default DS.Model.extend({ rev: DS.attr('string'), - name: DS.attr('string') + name: DS.attr('string'), + soup: DS.belongsTo('taco-soup', { async: true }) }); diff --git a/tests/dummy/app/serializers/application.js b/tests/dummy/app/serializers/application.js new file mode 100644 index 0000000..060aa04 --- /dev/null +++ b/tests/dummy/app/serializers/application.js @@ -0,0 +1,3 @@ +import { Serializer } from 'ember-pouch'; + +export default Serializer; diff --git a/tests/integration/adapters/pouch-basics-test.js b/tests/integration/adapters/pouch-basics-test.js index 8b8ee92..b92973b 100644 --- a/tests/integration/adapters/pouch-basics-test.js +++ b/tests/integration/adapters/pouch-basics-test.js @@ -114,6 +114,36 @@ test('create a new record', function (assert) { }); }); +test('creating an associated record stores a reference to it in the parent', function (assert) { + assert.expect(1); + + var done = assert.async(); + Ember.RSVP.Promise.resolve().then(() => { + return this.db().bulkDocs([ + { _id: 'tacoSoup_2_C', data: { flavor: 'al pastor', ingredients: [] } } + ]); + }).then(() => { + return this.store().findRecord('taco-soup', 'C'); + }).then(tacoSoup => { + var newIngredient = this.store().createRecord('food-item', { + name: 'pineapple', + soup: tacoSoup + }); + + return newIngredient.save().then(() => tacoSoup.save()); + }).then(() => { + this.store().unloadAll(); + + return this.store().findRecord('taco-soup', 'C'); + }).then(tacoSoup => { + return tacoSoup.get('ingredients'); + }).then(foundIngredients => { + assert.deepEqual(foundIngredients.mapBy('name'), ['pineapple'], + 'should have fully loaded the associated items'); + done(); + }); +}); + // This test fails due to a bug in ember data // (https://github.com/emberjs/data/issues/3736) // starting with ED v2.0.0-beta.1. It works again with ED v2.1.0.