PDA

View Full Version : How to write data in custom format



Ex_Soft
24 Jan 2012, 12:16 PM
I'm trying to write data in custom format:


function TestJson1()
{
Ext.define("TestJsonModel1", {
extend: "Ext.data.Model",
idProperty: "id",
fields: [
{ name: "id", type: "int" },
{ name: "name" }
]
});

Ext.define("TestJsonReader1", {
extend: "Ext.data.reader.Json",
alias: "reader.TestJsonReader1",
idProperty: "id",
root: "rows",
successProperty: "success",
totalProperty: "total",
read: function(response) {
if(window.console && console.log)
console.log("TestJsonReader1.read()");

var
data = [];

data=response;
data.rows.push({ "name": "Record# 5", "id": 5 });
data.total++;

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

Ext.define("TestJsonWriter1", {
extend: "Ext.data.writer.Json",
alias: "writer.TestJsonWriter1",
allowSingle: false,
root: "rows",
writeAllFields: false,
write: function(request) {
if(window.console && console.log)
console.log("TestJsonWriter1.write()");

return this.callParent(request);
},
writeRecords: function(request, data) {
if(window.console && console.log)
console.log("TestJsonWriter1.writeRecords()");

return this.callParent(request, data);
},
getRecordData: function(record) {
if(window.console && console.log)
console.log("TestJsonWriter1.getRecordData()");

return this.callParent(record);
}
});

var
store = Ext.create("Ext.data.Store", {
model: "TestJsonModel1",
proxy: {
type: "memory",
reader: "TestJsonReader1",
writer: "TestJsonWriter1"
}
}),
data={
"success" : true,
"total": 4,
"rows": [
{ "name": "Record# 1", "id": 1 },
{ "name": "Record# 2", "id": 2 },
{ "name": "Record# 3", "id": 3 },
{ "name": "Record# 4", "id": 4 }
]
};

store.loadRawData(data, false);

store.insert(0, Ext.create("TestJsonModel1", {
//id: 6,
name: "Record# 6"
}));

store.sync();
}

But any overridden methods of TestJsonWriter1 don't call.

lorezyra
24 Jan 2012, 6:13 PM
Why do you need to store the data in a custom format?

Why not output the basic data into an XTemplate?


If you must store the data in memory (with a custom format), consider using either Ext.util.HashMap (http://docs.sencha.com/ext-js/4-0/#%21/api/Ext.util.HashMap)or Ext.util.MixedCollection (http://docs.sencha.com/ext-js/4-0/#%21/api/Ext.util.MixedCollection)... Keep in mind the performance factors of each class.

Ex_Soft
25 Jan 2012, 12:09 AM
Why do you need to store the data in a custom format?
I exchange data with server in binary format. Moreover I get huge data. Therefore, when I get data, I ought to decode it from binary format, save in memory, load into store and show in grid. And when I e.g. update data in grid I ought to update data in memory, encode it to binary format and post to server. I use Ext.ux.data.PagingMemoryProxy. Does better way exist?