ember-data-model-fragments icon indicating copy to clipboard operation
ember-data-model-fragments copied to clipboard

default value on fragmentArray not working

Open jamesdixon opened this issue 8 years ago • 5 comments

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!

jamesdixon avatar Jan 23 '17 21:01 jamesdixon

@workmanw any thoughts on this one? Just ran into it again

jamesdixon avatar Mar 28 '17 03:03 jamesdixon

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.

workmanw avatar Mar 28 '17 10:03 workmanw

@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. screen shot 2017-05-01 at 22 06 57

m-basov avatar May 01 '17 19:05 m-basov

@kolybasov where did you end up adding this extension?

jamesdixon avatar May 12 '17 21:05 jamesdixon

@jamesdixon /app/transforms/fragment-array.js.

m-basov avatar May 13 '17 04:05 m-basov