Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

embedded associations are now automatically added to the parent's transa... #711

Merged
merged 1 commit into from

5 participants

@ghempton
Collaborator

...ction

Should fix #686

@thingista

Hi Gordon,

Sorry I might be confused... but how does it work to fix #686? Does it mean I have to explicitly create a transaction? I mean for instance, if I have a very simple model+controller like this:

App.Solution = DS.Model.extend({
sequences: DS.hasMany('App.Sequence', { embedded:'always' }),
});

App.SolutionsNewController = Ember.ObjectController.extend({
create: function(){
this.store.commit()
}
});

"create" still fires two POSTS to the server, even if the first one already sends all the necessary data (both solution and sequences data).

Thanks a lot
PJ

@ghempton
Collaborator

@thingista, yeah good point, I don't handle the default transaction here. This just solves for explicitly adding the parent to a transaction in which case all embedded records will be added as well.

@bobbus

@thingista , I think you are talking about issue #437 and #440 , for the #686 it should be fixed with this patch.
Thanks @ghempton !

@darthdeus
Collaborator

@ghempton this doesn't seem to merge without conflicts anymore :\

@ghempton
Collaborator

@darthdeus I just rebased, should work now

@stefanpenner stefanpenner merged commit 0e298f0 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
9 packages/ember-data/lib/system/transaction.js
@@ -132,6 +132,15 @@ DS.Transaction = Ember.Object.extend({
add: function(record) {
Ember.assert("You must pass a record into transaction.add()", record instanceof DS.Model);
+ var store = get(this, 'store');
+ var adapter = get(store, '_adapter');
+ var serializer = get(adapter, 'serializer');
+ serializer.eachEmbeddedRecord(record, function(embeddedRecord, embeddedType) {
+ if (embeddedType === 'load') { return; }
+
+ this.add(embeddedRecord);
+ }, this);
+
this.adoptRecord(record);
},
View
39 packages/ember-data/tests/integration/embedded/embedded_saving_test.js
@@ -2,12 +2,16 @@ var store, Adapter, adapter;
var Post, Comment, User, App;
var attr = DS.attr;
+var get = Ember.get, set = Ember.set;
+
+var originalLookup = Ember.lookup;
+
module("Embedded Saving", {
setup: function() {
- App = Ember.Namespace.create({ name: "App" });
+ App = Ember.lookup = Ember.Namespace.create({ name: "App" });
Comment = App.Comment = DS.Model.extend({
- title: attr('string'),
+ title: attr('string')
});
Post = App.Post = DS.Model.extend({
@@ -35,9 +39,40 @@ module("Embedded Saving", {
teardown: function() {
store.destroy();
App.destroy();
+ Ember.lookup = originalLookup;
}
});
+asyncTest("Modifying the parent in a different transaction", function() {
+ adapter.ajax = function(url, type, hash) {
+ equal(url, '/posts/1');
+ equal(type, 'PUT');
+ equal(hash.data.post.comments.length, 1);
+
+ return new Ember.RSVP.Promise(function(resolve, reject){
+ Ember.run.later(function(){
+ start();
+ resolve(hash.data);
+ },0);
+ });
+ };
+
+ adapter.load(store, Post, {
+ id: 1,
+ title: 'I cannot wait for Ember.Component to be implemented.',
+ comments: [{id: 2, title: 'yes!'}]
+ });
+
+ var post = store.find(Post, 1);
+
+ var t = store.transaction();
+ t.add(post);
+
+ set(post, 'title', "Hopefully soon.");
+
+ t.commit();
+});
+
asyncTest("Adding a new embedded record to an unsaved record: Both records use the same POST request.", function() {
adapter.ajax = function(url, type, hash) {
equal(url, '/posts');
Something went wrong with that request. Please try again.