I have a model with many attributes and certain associations. Most of them may or may not be null. I am trying to do an Ajax call with this model as a payload, and certain properties can be null.
I want to send only the properties that have values, and not null, including associations.
Is it possible? Kindly let me know if it is possible.
export default DS.Model.extend({
name : DS.attr('string'),
address : DS.hasMany('address')
});
For this you should override your serializer. If you use the default JSONAPISerializer
the right place is the serializeAttribute
hook:
export default DS.JSONAPISerializer.extend({
serializeAttribute(snapshot, json, key) {
if(snapshot.attr(key) != null) {
this._super(...arguments);
}
}
});
This will work for attributes, not relationships. However this is basically the same.
You can also write your own serializer. Then you just implement the serialize
hook, and write any code you want to create the JSON you want.