Waseem
Waseem

Reputation: 1408

How to populate Form with nested JSON in Extjs 4

I have a JSON like this

{
"success": true,
"users": [{ 
    "name":"Boom",
    "emails": [{
        "first": "[email protected]",
        "second": "[email protected]",
        "countries":[{
            "label":"pakistan",
            "continent":"asia"
            }]
        }]
}]

}

I have created my models for it like this

Ext.define('WR.model.WorkRecord', {
extend: 'Ext.data.Model',
fields: ['name'],      
hasMany: {model: 'WR.model.Email', name: 'emails'}


});


    Ext.define('WR.model.Email', {
        extend: 'Ext.data.Model',
        fields: ['first', 'second'],
        belongsTo: {model : 'WR.model.WorkRecord', name: 'users'},
        hasMany: {model: 'WR.model.Countries', name: 'countries'}
    });

    Ext.define('WR.model.Countries', {
        extend: 'Ext.data.Model',
        fields: ['label', 'continent'],
        belongsTo: {model: 'WR.model.Email', name: 'emails'}
    });

Now I want to populate my form having id formJobSummary .I did it successfuly for Non-Nested JSON by calling this function in store

listeners: {
    load: function(users) {         
        var form = Ext.getCmp('formJobSummary'); 
        form.loadRecord(this.data.first());
    }
}

My form has just simple displayfields and I want to populate them through this nested JSON thanks

Upvotes: 4

Views: 3916

Answers (2)

Tijuana
Tijuana

Reputation: 131

Currently you can't use name='property.subProperty' in a form field definition :(.

So in order to make this work, I revert the logic - add (a redundant) field definition to model:

Ext.define('WR.model.WorkRecord', {
  extend: 'Ext.data.Model',
  fields: [
    'name',
    {name: 'emailFirst', mapping: 'emails.first'}
  ],      
  hasMany: {model: 'WR.model.Email', name: 'emails'}   
});

then you can create a form field like:

{
  xtype: 'displayfield',
  name: 'emailFirst',
  ...
}

And it will be populated on form.loadRecord()

Upvotes: 4

A1rPun
A1rPun

Reputation: 16847

You need to subclass your Store and add the requires config.

Ext.define('MyJsonStore', {
    extend: 'Ext.data.JsonStore',
    requires: [
        'WR.model.WorkRecord'
    ]
});

Upvotes: 1

Related Questions