0
votes

Ok guys, working ExtJS4 and I have pretty much the exact opposite problem this guy did:
How do I get an ExtJS JsonStore to put the JSON data directly into the request body?

In my application I have a nested data structure being loaded with associated models.

CakePHP in the backend expects (in pseudocode) the following format in saves:

[
  {
    [Model]
      [Field1]: [Value1],
      [Field2]: [Value2],
      [Field3]: [Value3]
  },
  {
    [Model]
      [Field1]: [Value1],
      [Field2]: [Value2],
      [Field3]: [Value3]
  }
]

This is what ExtJS is currently sending to the server:

[
  {
    [Field1]: [Value1],
    [Field2]: [Value2],
    [Field3]: [Value3],
    [Model] : null
  }
]

So, for some reason it's including the model as a null value and putting the parameters in the root node of the Json object.

The server sends the data to the client in a similar fashion but the Json Reader for the Json Proxy has a record parameter (See here). Unfortunately the Json Writer has no such property (not even undocumented as I have tried setting it).

Currently my workaround on the back end is:
$json = Set::insert($json, 'Model', $json);
But it would be good if there was a better way to accomplish this.

Update: To further expand, I am loading one store which loads nested Json data that automatically populates associated models in the client. (This is all done with Extjs methods without any custom code).

I have tried the root property in the Json writer but unforunatly with batch records it formats the request like this:

{
  [Root] : [
    {
      [Field1]: [Value1],
      [Field2]: [Value2],
      [Field3]: [Value3],
      [Model] : null
    },
    {
      [Field1]: [Value1],
      [Field2]: [Value2],
      [Field3]: [Value3],
      [Model] : null
    }
  ]
}

I have also tried Bancha (http://banchaproject.org) but I was unable to get that to work with Sencha Architect (which is being used for this project) and the solution they have provided at the moment was a "workaround" (which I was unable to get working).

Thank you, -T6

2

2 Answers

1
votes

The thing with the modelName:null looks like you have some custom code already here. To just get the data in a named object, the Writer has a property 'root' in ExtJS and 'rootProperty' in Sencha Touch, this could help.

There's also a framework which does all of this marshaling for you, see banchaproject.org It costs a bit, similar to ExtJS, but it will do a lot of this work for you.

0
votes

In cases where I've needed to customize the format in which the Writer sends the request, I"ve simply created my own custom writer (extending Ext.data.writer.Json), and have overwritten the writeRecords() method with whatever logic I needed to format the request correctly.