PDA

View Full Version : Single record in store is not passed as an Array in Rest Sync with Batch enabled



rockskull
16 May 2014, 8:57 AM
Hello,

So I have the follwing model and store:



Ext.define('MyApp.Map.Marker', {
extend: 'Ext.data.Model',
idProperty: 'id',
fields: [
{name: 'id', type: 'int'},
{name: 'latitude', type: 'float'},
{name: 'longitude', type: 'float'},
]
});


var markerStore = Ext.create('Ext.data.Store', {
model: 'MyApp.Map.Marker',
proxy: {
type: 'rest',
batchActions: true,
appendId: false,
reader: { type: 'json', root: 'vertices' },
writer: {type: 'json', root: 'vertices'},
api: {
create: '/vertices/batch',
read: '/vertices',
update: '/vertices/batch',
destroy: '/vertices/batch'
}
},
listeners: {
wirte: function(){
console.log("I've written!");
}
}
});


So, as you can see, I configured to sync in batch, making only 3 request (one for create, one for update, and one for delete) instead of doing one for each modified record.

Everything is working fine except that when I have only one recordo to create, for example, my vertices param is a object instead of an array.

Like this. Two or more records:

"vertices"=>[{"id"=>0, "latitude"=>-22.891990243585727, "longitude"=>-47.20001220703125}, {"id"=>0, "latitude"=>-22.86668527847696, "longitude"=>-47.05101013183594}]

Single record

"vertices"=>{"id"=>0, "latitude"=>-22.916025590238377, "longitude"=>-47.330474853515625

Is there a way I can force ExtJS to put this single record in an Array?

Is that a normal Javascript behavior, or is just ExtJS? Because, for me, makes no sense, since it's a batch operation, to send the single record out of an Array.

rockskull
16 May 2014, 9:12 AM
So, I dug up a little bit in the ExtJS source codes, and I foudn that the sync() function of Ext.data.AbstractStore calls a function called getNewRecord()


toCreate = me.getNewRecords()
Ext.data.Store has this implementation of this fucntion:

getNewRecords: function() {
return this.data.filterBy(this.filterNew).items;
}
So tried this in the console:

array = markerStore.data.filterBy(markerStore.filterNew).items;
and I get an array with a single record!
So, the data acquisition is correct. Somewhere else is messing this around.

Scott Mildenberger
16 May 2014, 12:53 PM
I was messing with sync() today also and found the value 'allowSingle' in the writer config. Setting that to false sends a single record wrapped in an array.

rockskull
19 May 2014, 4:30 AM
Thanks! That Works.

I'm just curious why isn't this option documented in the Ext.data.writer.Writer documentation.

[EDIT]

I have to apologize. I found the allowSingle config in the Ext.data.writer.Json. I was, in fact, looking in the wrong place. The type 'json', in the writer make that writer an instance of the Ext.data.writer.Json.