2017-01-06 3 views
1

formTemplateオブジェクトにフォームIDの配列を追加したい場合を除いて、私は自分のスキーマをほぼ正確にどのように還元したいのかを設定しました。 それは次のようになります。ここでは それを持たないエンティティにIDの配列を追加するには

// Normalized Form Templates 
 
{ 
 
    1: { 
 
    id: '1', 
 
    isGlobal: true, 
 
    name: 'Form Template Name', 
 
    forms: [1, 2], // This is the line I want to add...but how? 
 
    }, 
 
} 
 

 
// Normalized Forms 
 
{ 
 
    1: { 
 
    id: '1', 
 
    createdAt: '2016-12-28T23:30:13.547Z', 
 
    name: 'Form 1', 
 
    parentTemplate: '1', 
 
    pdfs: [1, 2], 
 
    }, 
 
    2: { 
 
    id: '2', 
 
    createdAt: '2016-12-28T23:30:13.547Z', 
 
    name: 'Form 2', 
 
    parentTemplate: '1', 
 
    pdfs: [], 
 
    }, 
 
}
は私のスキーマ
import { schema } from 'normalizr' 
 

 
const formTemplate = new schema.Entity('formTemplates', {}, { 
 
    processStrategy: value => ({ 
 
     id: value.id, 
 
     name: value.attributes.title, 
 
     isGlobal: value.attributes.is_global, 
 
    }), 
 
}) 
 

 
const form = new schema.Entity('forms', { 
 
    pdfs: [pdf], 
 
}, { 
 
    processStrategy: value => ({ 
 
    id: value.id, 
 
    createdAt: value.attributes.created_at, 
 
    name: value.attributes.title, 
 
    parentTemplate: value.attributes.form_template_id, 
 
    pdfs: [...value.relationships.documents.data], 
 
    }), 
 
}) 
 

 
const pdf = new schema.Entity('pdfs') 
 

 
export default { 
 
    data: [form], 
 
    included: [formTemplate], 
 
}

これは私が正常化だというAPIレスポンスの例である

{ 
 
    "data": [ 
 
    { 
 
     "id": "5", 
 
     "type": "provider_forms", 
 
     "attributes": { 
 
     "title": "Form 1", 
 
     "created_at": "2017-01-02T06:00:42.518Z", 
 
     "form_template_id": 1 
 
     }, 
 
     "relationships": { 
 
     "form_template": { 
 
      "data": { 
 
      "id": "1", 
 
      "type": "form_templates" 
 
      } 
 
     }, 
 
     "documents": { 
 
      "data": [ // some pdf data here ] 
 
     } 
 
     } 
 
    } 
 
    ], 
 
    "included": [ 
 
    { 
 
     "id": "1", 
 
     "type": "form_templates", 
 
     "attributes": { 
 
     "title": "Form Template", 
 
     "created_at": "2016-12-29T22:24:36.201Z", 
 
     "updated_at": "2017-01-02T06:00:20.205Z", 
 
     "is_global": true 
 
     }, 
 
    } 
 
    ] 
 
}

答えて

0

フォームテンプレートエンティティが戻っフォームのエンティティへのコンテキストを持っていないので、あなたはNormalizrでこれを行うことはできません。あなたの行動/減速器はこれを処理する必要があります。

1

私はこれを行う方法を見つけました。私はそうのように手動でそれらをマッピングするために、私のformTemplateエンティティを変更:

const formTemplate = new schema.Entity('formTemplates', {}, { 
 
    processStrategy: (value, parent) => { 
 
    // eslint-disable-next-line eqeqeq 
 
    const childrenForms = parent.data.filter(form => form.attributes.form_template_id == value.id) 
 

 
    return { 
 
     id: value.id, 
 
     name: value.attributes.title, 
 
     isGlobal: value.attributes.is_global, 
 
     forms: childrenForms.map(form => form.id), 
 
    } 
 
    }, 
 
})

関連する問題