Skip to content

Instantly share code, notes, and snippets.

@typeoneerror
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save typeoneerror/9ea27728f7b452c426bb to your computer and use it in GitHub Desktop.
Save typeoneerror/9ea27728f7b452c426bb to your computer and use it in GitHub Desktop.
observing sub-properties of ember data object
{
"thing": "Thing",
"colors": {
"primary": "#000000",
"secondary": "#FFFFFF"
}
}
import Ember from 'ember';
import DS from 'ember-data';
var defaultColors = {
primary: '#8e247b',
secondary: '#5dd4fa'
};
export default DS.Transform.extend({
deserialize: function(serialized) {
if (Ember.isEmpty(serialized)) {
return Ember.Object.create(defaultColors);
}
return Ember.Object.create(serialized);
},
serialize: function(deserialized) {
return deserialized;
}
});
export default Model.extend({
thing: attr(),
// how can I dirty the model when colors.primary, colors.secondary changes?
colors: attr('colors')
});
@taras
Copy link

taras commented Feb 10, 2015

Observing nested properties this way is extremely problematic. You can keep pounding on this door, but you're going to have a bad time. You would be better off extracting properties from this nested object in the serializer with a goal to end up with

export default Model.extend({
  thing: attr(),
  // how can I dirty the model when colors.primary, colors.secondary changes?
  colorsPrimary: attr('string'),
  colorsSecondary: attr('string')
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment