adopted-ember-addons / ember-data-model-fragments

Ember Data addon to support nested JSON documents
MIT License
370 stars 114 forks source link

default value on fragmentArray not working #238

Open jamesdixon opened 7 years ago

jamesdixon commented 7 years ago

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 commented 7 years ago

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

workmanw commented 7 years ago

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.

m-basov commented 7 years ago

@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

jamesdixon commented 7 years ago

@kolybasov where did you end up adding this extension?

m-basov commented 7 years ago

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