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

Polymorphic not working?

未關閉
#249 2 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視
主要語言
TypeScript
星號
367
分支
108
PR 合併指標
30 天內沒有已合併 PR

描述

Hi, I've been trying to get polymorphic records to work for the past day with no success.

I believe I have followed the instructions correctly and I have searched the open and closed issues but still no go. They work fine to an extent. However when instantiating the object it always gives me the parent object properties only.

I have a section which has an array of custom fields. Those custom fields are polymorphic

models/section.js
```
export default Fragment.extend({
name: attr('string'),
fields: fragmentArray('custom-field', { polymorphic: true, typeKey: 'fieldTypeName', defaultValue: []})
});
```

model/custom-field.js
```
export default Fragment.extend({
fieldTypeName: attr('string'), //this comes in as custom-field-textbox
description: attr('string'),
```

model/custom-field-textbox.js
```
import CustomField from './custom-field';

export default CustomField .extend({
maxCharacterLimit: attr('number'),
});
```

serializers/custom-field.js
```
serialize(record, options) {
let json = this._super(...arguments);

if (record instanceof BookingFormFieldTextbox) {
json.maxCharacterLimit = record.get('maxCharacterLimit');
}

return json;
}
});
```

// serializers/custom-field-textbox.js
```
import BookingFormFieldSerializer from './booking-form-field';
export default BookingFormFieldSerializer;
```

The API sends:
``` [{"
MaxCharacterLimit":50,
"FieldTypeName":"custom-field-textbox",
"Description":"textbox limit 60"}]
```
After the normalize function is called what I have is something like:
```
{data: {
attributes: {
fieldTypeName: "custom-field-textbox",
fieldType 6,
identity: 1,
description: '',
},
type: 'custom-field'
}
```
However then calling maxCharacterLimit returns null.

Should the type be `custom-field` or `custom-field-textbox` ?

Also when I use createFragment('custom-field-textbox'..) it works fine and I can see and use the property maxCharacterLimit. I then do a PUT to save it and the model in Ember still shows the property. However when I refresh so that it reloads the data from the server it instantiates the custom-field object but no properties of the custom-field-textbox are seen.

Any ideas why this is not working?

Thanks in advance.

貢獻指南

開啟貢獻指南

評估

這個 Issue 還沒有評估資料。

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。