Results 1 to 2 of 2

Thread: Nested JSON in form.load

  1. #1
    Ext JS Premium Member
    Join Date
    Jan 2008
    Location
    Germany, Berlin
    Posts
    135

    Default Nested JSON in form.load

    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/showthre...rovement/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:

    Code:
    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:

    PHP Code:

    Ext
    .override(Ext.form.Field, {
        
    initComponentExt.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 
    0len values.lengthleni++){
                  
    valuesObject[values[i].id] = values[i].value;
              }
              return 
    this.setValues(valuesObject);
          }else{ 
    // object hash
              
    for (var 0items this.items.itemslen items.lengthleni++) {
                  var 
    field items[i];
                  var 
    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 ?

  2. #2
    Sencha User Condor's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    24,245

    Default

    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.

Similar Threads

  1. Display nested json value in form
    By riya_phoenix in forum Ext 3.x: Help & Discussion
    Replies: 0
    Last Post: 27 May 2010, 9:23 AM
  2. form.load show json response instead of form
    By Slapyo in forum Ext 2.x: Help & Discussion
    Replies: 14
    Last Post: 28 Feb 2009, 6:48 AM
  3. How to handle nested collections of fields on a form with json
    By robmonie in forum Ext 2.x: Help & Discussion
    Replies: 2
    Last Post: 3 Dec 2008, 2:39 AM
  4. Form load with nested objects (JsonReader)
    By Juvs in forum Ext 2.x: Help & Discussion
    Replies: 3
    Last Post: 15 Jun 2008, 12:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •