PDA

View Full Version : add data to nested store.



kjcr
11 Mar 2013, 8:44 PM
hello^^

i make model like this.


Ext.define('refDMC', {
extend: 'Ext.data.Model',
fields: [
{ name: 'DM', type: 'string' },
{ name: 'idx', type: 'string' },
],
hasMany:[
{
model:'subRefDMC',
name:'sub',
associationKey:'DM' } ] });

Ext.define('subRefDMC', {
extend: 'Ext.data.Model',
fields: [
{ name: 'DM', mapping:'record[0].DM' },
{ name: 'idx', mapping:'record[1].idx' },
],
association:[
{type:'belongTo',model:'refDM'}
]
});




and my store is



var refDMC = Ext.create('Ext.data.Store',{
model:'refDMC',
proxy:{
type:'memory'
},
autoLoad:false,
handler:{
load:function(store,rec){
console.log(store);
}
}
})


when i get data from server

the data is



{"items":[
{
"DM":"DMC-JHS182-AAAA-HD4-10-0001-00000-018A-A_001_EN",
"idx":21,
"DMS":[
{"DM":"DMC-JHS182-AAAA-HG7-00-0001-00000-053B-A_001_EN","idx":31}
]
},
{
"DM":"DMC-JHS182-AAAA-HG7-00-0001-00000-053B-A_001_EN",
"idx":31,
"DMS":[
{"DM":"DMC-JHS182-AAAA-HG7-00-0000-00000-413A-A_001_EN","idx":41},
{"DM":"DMC-JHS182-AAAA-HG7-00-0001-00000-420A-A_001_EN","idx":42},
{"DM":"DMC-JHS182-AAAA-HG7-00-0001-00000-420A-A_001_EN","idx":42},
{"DM":"DMC-JHS182-AAAA-HG7-00-0001-00000-420A-A_001_EN","idx":42}
]
}],
"success":false
}

how to i can insert data to my store?

please help.

mitchellsimoens
13 Mar 2013, 1:10 PM
2 big issues.

Your JSON has success = false and the mapping in the subRefDMS was bad:


Ext.define('refDMC', {
extend : 'Ext.data.Model',
fields : [
'DM',
'idx'
],
associations : [
{
type : 'hasMany',
model : 'subRefDMC',
name : 'sub',
associationKey : 'DMS'
}
]
});

Ext.define('subRefDMC', {
extend : 'Ext.data.Model',
fields : [
'DM',
'idx'
],
association : [
{
type : 'belongTo',
model : 'refDM'
}
]
});

Used this store locally:


new Ext.data.Store({
autoLoad : true,
model : 'refDMC',
proxy : {
type : 'ajax',
url : 'data/json.json',
reader : {
type : 'json',
root : 'items'
}
},
listeners : {
load : function (store, recs) {
var rec = recs[0],
sub = rec.sub();

console.log(sub);
}
}
});

kjcr
13 Mar 2013, 8:31 PM
i modified my code.

but still i don't know about add data to my store


Ext.ajax{(
type:'GET',
url:'user.json',
success:function(response){
//I don't know about here.
var json = Ext.decode(response.responseText);
var store = Ext.get('storeId');
store.add(json);//<-------how do i insert data to my nested store
},
failure:function(response){

}

)}


do you have any idea?:):):)