default value on fragmentArray not working
Hello,
I'm running into an issue where an attribute on my model that is set to a fragmentArray is always set to null by default rather than an empty array (default according to docs). I've tried setting in multiple ways, with no luck:
customRates: fragmentArray('rate', { defaultValue: function() {
return [];
}}),
customRates: fragmentArray('rate', { defaultValue: [] })
Here's the rate fragment I'm referencing:
// rate.js
import attr from 'ember-data/attr';
import Fragment from 'model-fragments/fragment';
import { validator, buildValidations } from 'ember-cp-validations';
const Validations = buildValidations({
type: validator('presence', true),
amount: [
validator('presence', true),
validator('number', {
allowString: true,
gt: 0,
lte: 100
})
],
serviceType: validator('presence', true),
serviceId: validator('presence', true)
});
export default Fragment.extend(Validations, {
type: attr('string'),
amount: attr('number'),
serviceType: attr(),
serviceId: attr('number')
});
I'm running ED 2.11 and EDMF 2.11.
Thanks in advance!
@workmanw any thoughts on this one? Just ran into it again
Do you think you could write either a failing test or an ember-twiddle for this? I could make some time to take a look.
@jamesdixon I had this issue as well because data passed to fragment-array transformation was null or undefined. I've made extension to this and now all is fine.
import FragmentArrayTransform from 'ember-data-model-fragments/transforms/fragment-array';
import { isNone } from 'ember-utils';
export default FragmentArrayTransform.extend({
deserialize(serialized) {
if (isNone(serialized)) {
return this._super([]);
}
return this._super(serialized);
}
});
@workmanw I think it should be default behaviour. Or at least use strict equality(===) to check for null. Now even undefined value will results to null so fragment cannot use default value for records from server.

@kolybasov where did you end up adding this extension?
@jamesdixon /app/transforms/fragment-array.js.