-
-
Save NuckChorris/927d7d4ba757abd26b30 to your computer and use it in GitHub Desktop.
In Ember-CLI, transforms are located in app/transforms/name.js
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
// app/transforms/array.js | |
import Ember from 'ember'; | |
import DS from 'ember-data'; | |
export default DS.Transform.extend({ | |
deserialize: function(value) { | |
if (Ember.isArray(value)) { | |
return Ember.A(value); | |
} else { | |
return Ember.A(); | |
} | |
}, | |
serialize: function(value) { | |
if (Ember.isArray(value)) { | |
return Ember.A(value); | |
} else { | |
return Ember.A(); | |
} | |
} | |
}); |
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
// app/transforms/object.js | |
import Ember from 'ember'; | |
import DS from 'ember-data'; | |
export default DS.Transform.extend({ | |
deserialize: function(value) { | |
if (!Ember.$.isPlainObject(value)) { | |
return {}; | |
} else { | |
return value; | |
} | |
}, | |
serialize: function(value) { | |
if (!Ember.$.isPlainObject(value)) { | |
return {}; | |
} else { | |
return value; | |
} | |
} | |
}); |
@billpull Point is, if you don't specify a type in your call to DS.attr()
, they'll be passed through as-is, and just work. I just tried this out myself and it worked fine.
Sort of late to the conversation, but I find these transforms useful if you want to use defaultValue with the attr. Otherwise you are correct, the objects just pass through when no attributes type is passed in.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@seanpdoyle
DS.attr('array')
andDS.attr('object')
are not supported by default in ember data only "string, number, boolean and date" so you have to define the transforms yourself.