0

I have defined a custom ComboBox component, that I want to re-use with multiple Stores. It has a simple config as below.

Ext.define('Myapp.CustomCombo', {
extend: 'Ext.form.ComboBox',
valueField: 'id',
displayField: 'name'
});

The model is

Ext.define('Myapp.model.ComboModel',{
extend: 'Ext.data.Model',
fields: [
    { name: 'id', type: 'int' },
    { name: 'name', type: 'string' },
    { name: 'linkCls', type: 'string' }
]
});

I define multiple stores each with its own proxy config & identified by a unique storeId, as below

Ext.create('Myapp.store.EmployeeComboStore', {
model: 'Myapp.model.ComboModel',
storeId: 'employeeLOVComboStore',
proxy: {
    type: 'rest',
    url: '/employees/getLovData',
    reader: {
        type: 'json',
        rootProperty: 'data'
    }
}
});

The server responds with a json as below

{
"data" : [
    {"employeeId": 1, "employeeName": "Chris"},
    {"employeeId": 2, "employeeName": "Jack"},
]
}    

I can have multiple such stores, like departmentStore with a different proxy URL and where in, the server response could be

{
"data" : [
    {"departmentId": 1, "departmentName": "Sales"},
    {"departmentId": 2, "departmentName": "Marketing"},
]
}

I want to define a mapping in the Reader, with instructions to map data differently in different stores, as in.. employeeId should be mapped to 'id' & employeeName to 'name' in employeeStore, whereas departmentId should be mapped to 'id' & departmentName to 'name' in departmentStore.

I've seen that there are options to define mapping for each field in a Model, but I would want to define a mapping in a Reader, since the server responds with data from a relational database where field names would be the columnNames.

bhates
  • 21
  • 1
  • 8
  • A couple options come to mind. 1.) Can you simply transform the data on the server to produce the genericized format? If the desire is to flatten multiple models into the same field names, the easiest approach would be to actually return data from the server that is in the desired format. 2.) If you don't want to do this, remember that stores can have implicit models created on the fly based on a fields[] config defined on the store itself. This, combined with mappings for the source fields, would allow you to flatten the store data into a common field architecture. – existdissolve Oct 23 '14 at 11:32

1 Answers1

1

What you can do, is to send along metaData from the server to the client.

The json response can look like this:

{
  data: [{ ... }],
  msg: "...",
  total: 99,
  metaData: {
    fields: [{ ... }],
    columns: [{ ... }],
    idProperty: "id",
    messageProperty: "msg",
    root: "data"
  }
}

In you case it would be sufficient if the json response would look like this

{
  data: [{ ... }],
  metaData: {
    fields: [
        { name: 'Id', type: 'int', mapping: 'departmentId' },
        { name: 'Name', type: 'string', mapping: 'departmentName' },
    ]
  }
}

Here is also a good example how it works: Basic Meta Data Config

harry
  • 672
  • 3
  • 13