PDA

View Full Version : Nested JSON in form.load



fschaeffer
5 Aug 2010, 10:32 PM
Hello all,

I (as many others before) try to load data to a form using the form.load and have in the backend an object which provides data as a nested JSON-object. As this is not part of the regular form.load I searched the forum and found a thread where a solution was posted (can't reply to this thread as it is a premium forum):
http://www.sencha.com/forum/showthread.php?50028-DISCUSS-Ext.form.BasicForm.setValues-improvement/page3

But there is one minor issue left which I am not able to solve, maybe you can put me on the right track.

In my response there is not always a certain object available.

Eg. one response contains data.pbsEasy.shorttext and the other one simply has no entries for that and so the part pbsEasy is omitted. On form.load everything works well as long as this element is present.
My form does have the field {name:pbsEasy.shorttext}.
When I now try to load the form for an item which does not contain the pbsEasy-part a javascript error arises:



pbsEasy is not defined
this.getLoadValue = new Function("...return " + tries.join("||") + ";}");


The function this.getLoadValue (from the thread mentioned above) simply tries to pass the value for that element but without success as the pbsEasy-part of the response is missing.

I don't have a clue though where to capture this error in the getLoadValue-function as tries in this case is simply empty and so I can't use Ext.isDefined to check the presence of tries in data.

Here is the solution from that thread again:




Ext.override(Ext.form.Field, {
initComponent: Ext.form.Field.prototype.initComponent.createSequence(function() {

// Generate a function which will extract the field's value from an object.
var tries = []
if (this.initialConfig.id) {
tries.push(this.initialConfig.id);
}
if (this.hiddenName) {
tries.push(this.hiddenName);
}
if (this.name) {
tries.push(this.name);
}
this.getLoadValue = new Function("data", "with(data){return " + tries.join("||") + ";}");
})
});

Ext.override(Ext.form.BasicForm, {
setValues : function(values){
if(Ext.isArray(values)){ // array of objects. Convert to object hash
var valuesObject = {};
for(var i = 0, len = values.length; i < len; i++){
valuesObject[values[i].id] = values[i].value;
}
return this.setValues(valuesObject);
}else{ // object hash
for (var i = 0, items = this.items.items, len = items.length; i < len; i++) {
var field = items[i];
var v = field.getLoadValue(values);
if(Ext.isDefined(v))
{
if ((typeof v !== 'undefined') && (v!=null)) {
field.setValue(v)
if(this.trackResetOnLoad){
field.originalValue = field.getValue();
}
}
}
}
}
return this;
}
});


Any idea how this.getLoadValue has to be modified to capture the error ?

Condor
5 Aug 2010, 11:44 PM
I think you are using the wrong solution.

You should be configuring a form 'reader' to process your data before it is used by the form.