Last active
August 29, 2015 14:15
-
-
Save typeoneerror/9ea27728f7b452c426bb to your computer and use it in GitHub Desktop.
observing sub-properties of ember data object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"thing": "Thing", | |
"colors": { | |
"primary": "#000000", | |
"secondary": "#FFFFFF" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default Model.extend({ | |
thing: attr(), | |
// how can I dirty the model when colors.primary, colors.secondary changes? | |
colors: attr('colors') | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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