2

I've been Googling for a while now, but haven't found any good solution.

The root of the problem is that my records aren't being set to isDirty when using this method:

DS.JSONTransforms.object = {
  deserialize: function(serialized) {
    return Ember.isNone(serialized) ? {} : serialized;
  },
  serialize: function(deserialized) {
    return Ember.isNone(deserialized) ? {} : deserialized;
  }
}

From what I gather this is an old method that apparently still works, since it handles the JSON objects I'm throwing at it, but it's not setting my records to isDirty when making edits.

What you now should be using is registerTransform on your adapter (according to this https://github.com/emberjs/data/issues/517). But my custom transform isn't being registered, so I guess I'm putting it at the wrong place (same place as my previous JSONTransforms).

DS.RESTAdapter.registerTransform('object', {
  deserialize: function(serialized) {
    return Em.none(serialized) ? {} : serialized;
  },
  serialize: function(deserialized) {
    return Em.none(deserialized) ? {} : deserialized;
  }
});

Any one have knowledge to share about this?

kroofy
  • 984
  • 2
  • 7
  • 21

2 Answers2

3

The issue with isDirty is not because you're not using registerTransform, the behavior will be the same.

For now, Ember Data does not support object attributes, one of the difficulty is in fact to observe the changes to set the isDirty flag.

There is an open issue for this that you can track on github.

A workaround would be to declare the nested objects as proper DS.Model and set an embedded relationship between them.

For the example, let's say you have a date object sended with a post :

{post: {
  id: 12
  title: "EmberData accept object attributes, you do not need this anymore !!"
  date: {
    day: "01"
    month: "03"
    year: "2013"
  }
}}

You will declare the model as following :

App.Post = DS.Model.extend({
  title: DS.attr('string'),
  date: DS.belongsTo('App.PostDate')
});

App.PostDate = DS.Model.extend({
  post: DS.belongsTo('App.Post'),
  day: DS.attr('string'),
  month: DS.attr('string'),
  year: DS.attr('string'),
});

And add a mapping in your adapter :

DS.RESTAdapter.map('App.Post',{
  date:{
    embedded:'always'
  }
})

See this answer for more detail about the embedded mapping option.

Community
  • 1
  • 1
Adrien Coquio
  • 4,870
  • 2
  • 24
  • 37
  • Thanks for the clarification. The problem I'm facing is that my JSON returns an Object with unknown attributes. So I guess it's not really possible to define a Model for me in this case. But now I know how to design around this issue at least, thanks again. – kroofy Feb 05 '13 at 05:45
  • How to tackle deep cases http://stackoverflow.com/questions/14896049/emberjs-multi-level-hierarchy-with-embedded-always – sudhanshu Feb 15 '13 at 14:17
0

I disagree. I think its inefficient and overly complex to have to have extra calls for all your child relationships/records. I found a work around for this. Add a field for 'last_updated' and after you change the value of your nested stuff, just update the last_updated to the current time, that will trigger the 'isDirty' flag and allow you to update...

Ben
  • 16,124
  • 22
  • 77
  • 122