PDA

View Full Version : Loding nested data from xml to model (or store)



spot62
26 Nov 2012, 2:11 AM
Hello, I need load data from nested xml to model (or store).
For xml:


<?xml version="1.0" encoding="UTF-8"?>
<graph id="1" name="Voltage" >
<d t="1353923747" v="32" />
<d t="1353923807" v="57" />
<d t="1353923867" v="73" />
<d t="1353923927" v="28" />
</graph>

I try this code (as in the guide)

Ext.define('MyGraph', {
extend: 'Ext.data.Model',
fields: [
{name: 'id', type: 'integer', mapping : '@id'},
{name: 'name', type: 'string', mapping : '@name' }
],
proxy: {
type: 'ajax',
actionMethods: 'POST',
noCache: false,
pageParam : '',
startParam: '',
limitParam: '',
extraParams: {
querytype: 'getgraph',
},
url : 'server/graph.php',
reader:{
type: 'xml',
record: 'graph',
root : 'graph'
}
},

hasMany: 'MyData' // shorthand for { model: 'MyData', name: 'mydatas' }
});

Ext.define('MyData', {
extend: 'Ext.data.Model',
fields:[
{name: 'time', type: 'integer', mapping : '@t', dateFormat: 'timestamp'},
{name: 'value', type: 'float', mapping : '@v' }
],
belongsTo: 'MyGraph'
});

MyGraph.load(1, {
success: function(elem) {
console.log("Graph: " + elem.get('name'));
console.log(elem);

elem.mydatas().each(function(data) {
console.log("Data: " + data.get('value'));
});
}
});

But store "mydatas" is empty after load. Why? How repair this?
Thank you.

spot62
29 Nov 2012, 7:42 AM
Solving:

Ext.define('MyGraph', {
extend: 'Ext.data.Model',
fields: [
{name: 'id', type: 'integer', mapping : '@id'},
{name: 'name', type: 'string', mapping : '@name' }
],
proxy: {
type: 'ajax',
actionMethods: 'POST',
noCache: false,
pageParam : '',
startParam: '',
limitParam: '',
extraParams: {
querytype: 'getgraph',
element: 1000
},
url : 'server/graph.php',
reader:{
type: 'xml',
record: 'graph',
root : 'graph'
}
},

//hasMany: 'MyData' // shorthand for { model: 'MyData', name: 'mydatas' }
hasMany: { model: 'MyData', name: 'time' }
});

Ext.define('MyData', {
extend: 'Ext.data.Model',
fields:[
{name: 'time', type: 'integer', mapping : '@t', dateFormat: 'timestamp'},
{name: 'value', type: 'float', mapping : '@v' }
],proxy: {
type: 'memory',
reader: {
type: 'xml',
root: 'ds',
record: 'd'
}
},
belongsTo: 'MyGraph'
});

MyGraph.load(1, {
success: function(elem) {
console.log("Graph: " + elem.get('name'));
console.log(elem);

elem.time().each(function(data) {
console.log(data.get('value')+':'+data.get('time'));
});
}
});
and add node "ds" for xml:


<?xml version="1.0" encoding="UTF-8"?> <graph id="1" name="Voltage" >
<ds> <d t="1353923747" v="32" /> <d t="1353923807" v="57" /> <d t="1353923867" v="73" /> <d t="1353923927" v="28" /> </ds>
</graph>