0
votes

I want to load nested JSON in a Store, but the data is not correctly mapped. No problem with a single regModel but I canĀ“t get the associations to work.

// JSON in test.json

{"message" : {

"returnCodes": [
        {
            "value": "0",
            "code": "200",
            "description": "OK"
        },
        {
            "value": "0",
            "code": "200",
            "description": "OK"
        }
    ]
}}

// Model with associations 

Ext.regModel("ReturnCode", {

    fields : [{
        name : "value",
        type : "string"
    }, {
        name : "code",
        type : "string"
    }, {
        name : "description",
        type : "string"
    }],

    belongsTo: "Message"
});

Ext.regModel("Message", {

    hasMany: { 
         model : "ReturnCode", 
         name : "returnCodes" 
    }
});

// Store
var jobStore = new Ext.data.Store({

model : 'Message',
autoLoad: true,

proxy : {
type : 'ajax',
url: 'test.json',

reader : {
    type : 'json',
    root : 'message.returnCodes'         
}
}});

// List
var list = Ext.extend( Ext.List, {

fullscreen : true,
store : jobStore,
grouped : false,
itemTpl : '<div>{code}</div>'      // no output

});

When I look into the store every data is stored in the raw section of the store object but nothing in the data section. In the list for both returnCode Objects a listitem is created but they are not filled with data, because the mapping didn't succeed -> itemTpl gets no data.

1

1 Answers

0
votes

Try declaring Message Model first, then add associationKey:'returnCodes' in hasMany{} within the Message Model. Also change the root of the reader to message.

This reference could also be of use to you.