PDA

View Full Version : How to work with server response in custom format



Ex_Soft
18 Jan 2012, 6:01 AM
I get data from server in custom format. Is there any way to pass it to a Ext.data.Store?

mitchellsimoens
18 Jan 2012, 6:43 AM
The reader is what transforms the response into a JS array for the store.

friend
18 Jan 2012, 6:51 AM
If your data is some non-standard format and there's no Ext.data.reader.* class which directly supports it, you have two options:


Extend Ext.data.reader.Reader and roll-your-own.
Use an Ext.Ajax.request() to retrieve the data, then massage it manually and populate the target store via it's loadData([]) method. See below for this approach:




Ext.Ajax.request({
url: 'employees/list',
params: {
someParam: 'someValue'
},
success: function(response){
// this assumes the response from the server can be decoded into
// valid JSON. If not, then you'll have to directly parse the text value
// contained in response.responseText.
var json= Ext.decode(response.responseText);

// pluck out the collection/values to be used with your Store,
// and call the stores.loadData([]) method appropriately.

}
});

Ex_Soft
19 Jan 2012, 1:17 AM
Extend Ext.data.reader.Reader and roll-your-own


function TestArray3()
{
Ext.define("TestArrayModel3", {
extend: "Ext.data.Model",
idProperty: "id",
fields: [
{ name: "id", type: "int", mapping: 1 },
{ name: "name", mapping: 0 }
]
});

Ext.define("TestArrayReader3", {
extend: "Ext.data.reader.Array",
model: "TestArrayModel3"
});

var
store = Ext.create("Ext.data.Store", {
model: "TestArrayModel3",
proxy: {
type: "memory",
reader: Ext.create("TestArrayReader3")
}
}),
data=[
[ "Record# 1", 1 ],
[ "Record# 2", 2 ],
[ "Record# 3", 3 ],
[ "Record# 4", 4 ]
],
tmpRec;

store.loadRawData(data, false);

store.each(function(r)
{
if(window.console && console.log)
console.log("id=%i name=\"%s\"", r.get("id"), r.get("name"));
});

tmpRec=store.getById(1);
if(tmpRec)
{
if(window.console && console.log)
console.log("Store.getById(): id=%i name=\"%s\"", tmpRec.get("id"), tmpRec.get("name"));
}
}



config is undefined
me.model = Ext.ModelManager.getModel(config.model); ext-all-debug.js(line 25126)

Ex_Soft
19 Jan 2012, 4:16 AM
function TestArray3()
{
Ext.define("TestArrayModel3", {
extend: "Ext.data.Model",
idProperty: "id",
fields: [
{ name: "id", type: "int", mapping: 1 },
{ name: "name", mapping: 0 }
]
});


Ext.define("TestArrayReader3", {
extend: "Ext.data.reader.Array",
alias: "reader.TestArrayReader3",
read: function(response) {
var
data = [];

data=DoSmth(response);

return this.callParent([data]);
}
});


var
store = Ext.create("Ext.data.Store", {
model: "TestArrayModel3",
proxy: {
type: "memory",
reader: "TestArrayReader3"
}
}),
data=[
[ "Record# 1", 1 ],
[ "Record# 2", 2 ],
[ "Record# 3", 3 ],
[ "Record# 4", 4 ]
],
tmpRec;


store.loadRawData(data, false);

store.each(function(r)
{
if(window.console && console.log)
console.log("id=%i name=\"%s\"", r.get("id"), r.get("name"));
});


tmpRec=store.getById(1);
if(tmpRec)
{
if(window.console && console.log)
console.log("Store.getById(): id=%i name=\"%s\"", tmpRec.get("id"), tmpRec.get("name"));
}
}