PDA

View Full Version : How to define the Model and Load the store for this Complex Json



nannuru
13 Sep 2012, 1:00 PM
{

field1: "value1",
field2: "value2",
field3: "value3",
field4: "value4",
field5: "value5",

-details: {

field6: "value6",

field7: "value7",


}
-
adddetails: [

-
{

field8: "value8",

field9: "value9",


}
-
{

field8: "value10",

field9: "value11",


}

]
}

crysfel
13 Sep 2012, 1:11 PM
You can use the mapping property of the fields to specify the path from the value will be extracted.



Ext.define('MyApp.model.MyModel',{
extend : 'Ext.data.Model',

fields : [
{name:'field6',mapping:'details.field6'},
{name:'field7',mapping:'details.field7'}
]
});


For the array you can create a one-to-many association. You can find more information on the documentation: http://docs.sencha.com/ext-js/4-1/#!/api/Ext.data.Model

Regards

nannuru
13 Sep 2012, 1:20 PM
Thanks for the reply .But in the store it is not getting the values.

scottmartin
14 Sep 2012, 8:03 AM
Your entries 'details' is an object and you 'adddetails' is an array
Was this Intentional?

so you would need to use:

{ mapping:'adddetails[0].fieldx' }

You can also use the qualifier in you field/data index when using objects



dataIndex: 'details.field6'
fields:['details.field6'],


If you need to separate.

Scott.

nannuru
14 Sep 2012, 6:51 PM
Ext.define('app.model.MYModel', {
extend: 'Ext.data.Model',

id:'MYModel',


config: {
dataIndex: 'details.filed6',
fields: [
{
name: 'filed1',
type:'string'
},
{
name: 'filed2',
type:'string'
},
{
name: 'filed3',
type:'string'
},
{
name: 'filed4',
type:'string'
},
{
name: 'filed4',
type:'string'
},


{
name: 'filed6',


type:'string'
}
]


}
});

Is the above model is fine for the JSOn mentioned above?

sword-it
15 Sep 2012, 2:59 AM
@nannuru,

your model are not valid, the valid model for above mentioned json are -



Ext.define('MainModel', {
extend:'Ext.data.Model',

fields:[
{name:'field1'},
....................,
{name:'field5'},
{name:'field6', mapping:'details.field6'},
{name:'field7', mapping:'details.field7'}
],
hasMany:{model:'Address', name:'adddetails'}
})


Ext.define('Address', {
extend:'Ext.data.Model',
fields:[
{name:'field8'},
{name:'field9'}
],
belongsTo:'MainModel'
})

nannuru
15 Sep 2012, 4:34 AM
code:-




Ext.define('app.model.MYModel', {
extend: 'Ext.data.Model',

id:'MYModel',


config: {
dataIndex: 'details.filed6',
fields: [
{
name: 'filed1',
type:'string'
},
{
name: 'filed2',
type:'string'
},
{
name: 'filed3',
type:'string'
},
{
name: 'filed4',
type:'string'
},
{
name: 'filed4',
type:'string'
},


{
name: 'filed6',


type:'string',
mapping: 'details.filed6'
}
{
name: 'filed7',


type:'string',
mapping: 'details.filed7'
}
]


}
});





Store:-





Ext.define('app.store.MYStore', {
extend: 'Ext.data.Store',
id:'MYStore',
config: {
model: 'app.model.MYModel',

proxy: {
type: 'ajax',
url: /tempjson
reader: {
type:'json',

},
writer: {
type : 'json',

writeAllFields: true,
nameProperty : 'mapping',
successProperty : 'success'

}
},
autoLoad: true,
timeout: 5000,


}});





View:-




Ext.define('app.view.MYPanel', {
extend: 'Ext.form.FormPanel',
xtype: 'MYFormPanel',
id:'MYFormPanel',
config: {
items: [


{
xtype: 'fieldset',
id:'myfiledset',
title: 'MyJSONfirstlist',
}
items: [


{
xtype: 'textfield',
label: 'field1:',
name: 'field1',
},]},


{
xtype: 'fieldset',
id:'myfiledset',
title: 'MyJSONfirstlistdetails',
}
items: [


{
xtype: 'textfield',
label: 'field6:',
name: 'field6',
},


xtype: 'textfield',
label: 'field7:',
name: 'field7',
}]},


Controller:=




this.getMyform().setRecord(Ext.getStore("MYStore").getAt(0));


filed6 and filed 7 are not getting loaded.