From 68730fef529b9bbd6b29df59f4f09d64c040f7d6 Mon Sep 17 00:00:00 2001 From: Craig Teegarden Date: Fri, 15 Nov 2013 15:33:46 -0500 Subject: [PATCH] Tests showing creating and saving a model with async relationships does not POST the relationship to the server. The issue seems to be that a model with a relationship attribute that is marked as async: true will return a promise when that attribute is accessed using get(). This causes the serializeBelongsTo() or serializeHasMany() method in the serializer to get() the relationship object and incorrectly assume it is the actual model object, when a promise was instead retrieved. For example, with an async: true relationship, this line is trying to get the id of the related model object but it is instead accessing the id property of a promise, which returns undefined: https://github.com/toranb/ember-data-django-rest-adapter/blob/master/src/serializer.js#L123 --- tests/adapter_tests.js | 68 ++++++++++++++++++++++++++++++++++++++++++ tests/app.js | 3 +- 2 files changed, 70 insertions(+), 1 deletion(-) diff --git a/tests/adapter_tests.js b/tests/adapter_tests.js index 6cf7995..a8413c8 100644 --- a/tests/adapter_tests.js +++ b/tests/adapter_tests.js @@ -370,3 +370,71 @@ test('camelCase belongsTo key is serialized with underscores on save', function( equal(ajaxHash.data, '{"description":"firstkid","camel_parent":"1"}'); }); }); + +asyncTest('async hasMany relationship is serialized', function() { + var store, + assertion; + + stubEndpointForHttpRequest('/api/camels/', {}, 'POST'); // avoid a failed request + + store = App.__container__.lookup('store:main'); + Ember.run(App, function(){ + store.serializerFor('tag').pushSinglePayload(store, 'tag', {"id": 10, "description": "django"}); + }); + + visit("/").then(function() { + return store.find('tag', 10); + }).then(function(tag){ + var camel; + camel = store.createRecord('camel', { + camelCaseAttribute: 'awesome stuff in here', + camelCaseRelationship: [tag], + belongsToAsyncRelationship: null + }); + + return camel.save().then( + assertion, + assertion + ); // check the assertion regardless of success or failure of camel.save() + }); + + assertion = function() { + equal(ajaxHash.data, '{"camel_case_attribute":"awesome stuff in here","camel_case_relationship":["10"],"belongs_to_async_relationship":null}'); + start(); // end the asyncTest + }; + +}); + +asyncTest('async belongsTo relationship is serialized', function() { + var store, + assertion; + + stubEndpointForHttpRequest('/api/camels/', {}, 'POST'); // avoid a failed request + + store = App.__container__.lookup('store:main'); + Ember.run(App, function(){ + store.serializerFor('tag').pushSinglePayload(store, 'tag', {"id": 10, "description": "django"}); + }); + + visit("/").then(function() { + return store.find('tag', 10); + }).then(function(tag){ + var camel; + camel = store.createRecord('camel', { + camelCaseAttribute: 'awesome stuff in here', + camelCaseRelationship: null, + belongsToAsyncRelationship: tag + }); + + return camel.save().then( + assertion, + assertion + ); // check the assertion regardless of success or failure of camel.save() + }); + + assertion = function() { + equal(ajaxHash.data, '{"camel_case_attribute":"awesome stuff in here","camel_case_relationship":[],"belongs_to_async_relationship":"10"}'); + start(); // end the asyncTest + }; + +}); diff --git a/tests/app.js b/tests/app.js index 22cf8dd..85f6551 100644 --- a/tests/app.js +++ b/tests/app.js @@ -38,7 +38,8 @@ App.CamelUrl = DS.Model.extend({ App.Camel = DS.Model.extend({ camelCaseAttribute: DS.attr('string'), - camelCaseRelationship: DS.hasMany('tag', { async: true }) + camelCaseRelationship: DS.hasMany('tag', { async: true }), + belongsToAsyncRelationship: DS.belongsTo('tag', { async: true }) }); App.Location = DS.Model.extend({