mardi 26 juin 2018

Use variable for array key naming inside Ember class

I have an Ember component class as below;

import Ember from 'ember';
import myappconstants from '../utils/constants';

export default Ember.Component.extend({
    constants: myappconstants.FIELD_CONSTANTS.SECTION_1,
    myConfigs: {
        [myappconstants.FIELD_CONSTANTS.SECTION_1.FIELD_1] : {fieldName:"my__label__1", type:"text"},
        [myappconstants.FIELD_CONSTANTS.SECTION_1.FIELD_2] : {fieldName:"my__label__2", type:"text"}        
    }
})

My question is in my component class, I want to have the "myConfigs" keys defined in a slightly different way. This is because the definition can grow to around as many as 20 items & any name change in future would have to be done in multiple places.

So I want it to be defined as

myConfigs: {
        [this.constants.FIELD_1] : {fieldName:"my__label__1", type:"text"},
        [this.constants.FIELD_2] : {fieldName:"my__label__2", type:"text"}      
    }

With the above code, I get an error; Cannot read property 'constants' of undefined

Is it possible to achieve the above naming ?




Aucun commentaire:

Enregistrer un commentaire