PDA

View Full Version : Can a proxy or reader redefine a model's fields using a JSON response?



donovanhide
30 Nov 2011, 6:37 PM
Given a simple MVC model:




Ext.define('app.model.Document', {
extend: 'Ext.data.Model',
fields: ['id'],
proxy: {
type: 'rest',
url: '/document/',
reader: {
type: 'json',
root: 'documents',
}
}
});



could a JSON response from http://server/document/ like:




{
"success" : true,
"fields" : ['id','doctype','docid','characters','group','title'],
"documents" : [
{

"characters":"1941642",
"docid":"1",
"doctype":"2",
"group":"Dickens",
"title":"bleak_house.txt"
},
{
"characters":"1742216",
"docid":"9",
"doctype":"2",
"group":"Dickens",
"title":"pickwick_papers.txt"
}
]
}



use the fields property to redefine the model to have the additional specified fields? Example controller usage might be:




......
var doc=this.getDocumentModel.load(1,{
success: function(record,operation){
console.log(record.title);
}
});
.......



The Document fields are dynamic server-side and each request might return more or less fields. I can see many methods in both the proxy and reader classes which can be overridden, but am unsure which one would be the best to use.


Thanks for any help!


Donovan.

mitchellsimoens
1 Dec 2011, 6:51 AM
If you can get the model you can change the prototype field property. Just make sure you have done it properly. Look at how it is before you change it and make it look like that.

donovanhide
1 Dec 2011, 7:03 AM
Hi Mitchell,

thanks for the reply! In which method of either the proxy or reader (or even model) would you say is the best place to change the fields prototype? The Operation class seems to hold all the necessary response information, but it has both a resultset and a record field. Do I need to update both? Bit confused :)

Cheers,
Donovan.