Ember js cloning record with relations


#1

What I try to achieve: user goes to post/3/edit. Here the form is bound to the model, so it already filled out. And on the top

You are editing the post {{model.title}}

If the user changes the title it changes dynamic. I don’t want this behavior. I want to apply the changes to the record, if user already hit the “Save” button (and everything went right on server side).

my idea:

  1. clone the record
  2. bind cloned record to the form
  3. after hit “Save” the properties of the cloned (and edited) record are applied to the original record.

Questions

  • Is this the right way to do this?
  • Is there something like record.clone(), which deep copy the record with it’s relations?
  • Is there a way to apply the changes, like originalRecord.apply(clonedRecord)?

#2

There’s this post on Stackoverflow that could help:


#3

Have a look at BufferedProxy:


#4

Thank you! BufferedProxy does exactly what I need, but there is a problem. I use https://github.com/dockyard/ember-validations which is a Mixin. I enrich my models with it, so I will have .validate() on them. But it looks BufferedProxy will work only on properties.

var model = this.get('model');
model.get('title'); // "Some title"
model.validate(); // works
var buffer = Ember.ObjectProxy.extend(BufferedMixin, EmberValidations).create({
  content: model
});
buffer.get('title'); // "Some title" <- nice
buffer.validate(); // Uncaught TypeError: undefined is not a function

#5

Why not to use a an Ember model with the same structure than the data model, and once user hits save, copy the new values to the actual data model?


#6

I could do that one by one on all properties at each editing form, but I was looking for something more automated. I ended up reopen DS.Model and write my own .clone() function, as @splattne suggested. I was just wondering why this is not part of ember data.