PDA

View Full Version : JSON Proxy, create multiple record sync issue.



WillFM
24 Apr 2012, 7:13 AM
I'm having an issue with creating multiple new records in a store (extjs 4.1), and syncing them with the server.

When I send the create request, I'm returning an identical array, with the newly assigned id from the server appended into the id property, but the id does not want to propagate back to the store. It works fine with single Items, that aren't when there's multiple items in the store to be created in the sync:

Example:


/***Works***/
//Request:
{
Data: {"id":"","text":"Foo bar"}
}

//Response:
{
Data: {"id":"1","text":"Foo bar"},
"success": true
}



/***Doesn't work***/
//Request:
{
Data: [
{"id":"","text":"Foo bar"},
{"id":"","text":"Bar foo"}
]
}
//Response:
{
Data: [
{"id":"1","text":"Foo bar"},
{"id":"2","text":"Bar foo"}
],
"success": true
}


Any clue what I could be doing wrong? does it expect it in a different format then above?

scottmartin
24 Apr 2012, 10:12 AM
This is not possible.
You will need to issue an ajax request to send the data and then call store.load() to see the changes.

Regards,
Scott.

WillFM
24 Apr 2012, 10:46 AM
Scott, thank you for the reply.

Just to make sure you understand the question, the example I gave, (response ways), is the JSON, that ext generates itself.

I don't understand why it would be designed to update when a single record is sync'd on a store, but would not do the same, when several items, were sent. I would think this would defeat the purpose, of the store sync command, to sync all changes to the store with the database, and vise versa .

What would be your suggestion then to update these, I would think your store load(); suggestion, would leave the old records in tact, and then in case return "duplicate" records. sans's the previously created records lacking the Id's I'm trying to obtain from the server. I guess I could just search the store for records after the sync, that don't have an id. remove them, and then do the load(). the issue with that, is then the old records, would be marked as removed, and would want to destroy() on the next time I called sync(). Would there be away to delete these without making the store dirty?

scottmartin
24 Apr 2012, 2:10 PM
Updating multiple is fine, see:
http://dev.sencha.com/deploy/ext-4.1.0-gpl/examples/writer/writer.html

If you edit a record that has off id it will simulate an error. If you then edit an id with an even id, it will update both as expected. You can see the response.

The issue you are addressing is new records ... All the sent id's are "" and the server returns them as 1,2 ...

Scott.

vietits
24 Apr 2012, 5:40 PM
@WillFM,

It seems that you are using Ext 4.1. If this is the case and you want to create multiple records, you should config clientIdProperty for your model. Ex:


Ext.define('MyModel', {
clientIdProperty: 'clientId',
....
})

Then when you sync with server, each record in the request will automatically include an client id as below:


{
Data: [
{"id":"","text":"Foo bar", "clientId":"ext-record-XX"},
{"id":"","text":"Bar foo", "clientId":"ext-record-YY"}
]
}

Your returned data from server should include client id for each record to update the client side.


{
Data: [
{"id":"1","text":"Foo bar", "clientId":"ext-record-XX"},
{"id":"2","text":"Bar foo", "clientId":"ext-record-YY"}
],
"success": true
}

Izhaki
25 Jun 2012, 8:02 AM
@WillFM,

It seems that you are using Ext 4.1. If this is the case and you want to create multiple records, you should config clientIdProperty for your model.


@vietits, I cannot thank you enough for this reply. I have been debugging the framework for 4 hours and honestly wouldn't know what to do had I not found this thread.

conorarmstrong
8 Aug 2012, 1:17 PM
I think it is important to note here that even if the clientIdProperty config is set, then the phantom id is ONLY sent if more than one record is sent to the server. If only a single record is sent, then the phantom id is not sent. Server side code will have to deal with this correctly.

Spent the past hour pulling my hair out looking for the property in the JSON sent to the server when only saving one record. You need to save two or more to see it.