PDA

View Full Version : Restful post missing brackets on single record



shaleth
10 Jun 2013, 1:30 PM
I'm in the very early stages of developing an app. I'm using a store that calls a restful web services API, which I'm also writing... The issue I'm having is this - when I send a single record, brackets seem to be missing from the JSON that is sent back to the php code. The root on the model is "ERRORLOGS"

For example:
logStore.add(


Ext.create('Tim3.model.Log', {

"LOGIN": "shaleth",
"PROC_NAME": "MyProc",
"ERR_MSG": "logging test"

})

);
logStore.sync({


success: function(){ console.log('success'); },
failure: function(){ console.log('failure'); },
scope: this

});

sends:

{"ERRORLOGS":{"LOGIN":"shaleth","PROC_NAME":"MyProc","ERR_MSG":"logging test"}}

When I add a second record to the store and then sync, it works correctly:


logStore.add(


Ext.create('Tim3.model.Log', {


"LOGIN": "shaleth",
"PROC_NAME": "MyProc",
"ERR_MSG": "logging test"

})

);
logStore.add(


Ext.create('Tim3.model.Log', {


"LOGIN": "shaleth",
"PROC_NAME": "MyProc",
"ERR_MSG": "another logging test"

})

);
logStore.sync({


success: function(){ console.log('success'); },
failure: function(){ console.log('failure'); },
scope: this

});


returns:
{"ERRORLOGS":[{"LOGIN":"shaleth","PROC_NAME":"MyProc","ERR_MSG":"logging test"},{"LOGIN":"shaleth","PROC_NAME":"MyProc","ERR_MSG":"another logging test"}]}


Note the brackets starting after the first colon - those are missing in the single record example. PHP barks on the single record example - json_decode handles the two situations very differently. What am I missing here? Is this expected behavior?

existdissolve
10 Jun 2013, 2:06 PM
This is expected behavior. If you want to submit even single records as an array, you should set allowSingle: true on your writer: http://docs.sencha.com/extjs/4.2.1/#!/api/Ext.data.writer.Json-cfg-allowSingle

H (http://docs.sencha.com/extjs/4.2.1/#!/api/Ext.data.writer.Json-cfg-allowSingle)ope this helps!

shaleth
10 Jun 2013, 2:10 PM
Brilliant - thank you so much! Setting it to false did the trick.

existdissolve
10 Jun 2013, 2:14 PM
Brilliant - thank you so much! Setting it to false did the trick.

Cool, glad it was helpful!