PDA

View Full Version : How does a server respond to a partially successful CRUD Operation



JonathanStone
22 Mar 2014, 5:52 AM
I have written an ExtJS 4.2.2 CRUD database application using a Store configured with an AJAX proxy and JSON Reader.




I would like to create several records in my database from one Store.sync call. However, I do not understand how the server should respond when some of the records can be created and some cannot. For example when attempting to create two records the server might respond as follows when both records are successfully created:


{
success: true,
users: [
{ id: 1, name: ‘Ed’ },
{ id: 2, name: ‘Tommy’}
]
}






But how should the server respond if one of the records can be created but the other cannot?

Thanks for any help.

scottmartin
22 Mar 2014, 6:21 AM
The response is up to you, and how you handle it. On fail, you would return success: false and message: 'reason for fail'.

You would need to decide on the server to keep or rollback changes, etc.

JonathanStone
22 Mar 2014, 7:10 AM
Hi Scott


Thanks for the quick reply, but I am still a little unclear.
For the standard ExtJS AJAX Proxy and JSON Reader I thought the response had to be in the above format and hence they cannot deal with partial success/failure.


Are you suggesting that I sub-class the AJAX Proxy and interprete the server response in a way specific to my needs? If this is what I should do I am ok with it. I just don’t won’t to be missing out on using the standard AJAX proxy if it can handle partial success/failure out of the box.

Jonathan

scottmartin
22 Mar 2014, 10:03 AM
If you send 3 records to the server, and 2 fail .. do you consider that a success or fail?
Aside for what is going on, describe what you expect to happen when a fail happens .. just in general.

JonathanStone
22 Mar 2014, 10:34 AM
Hi Scott


Your question: “If you send 3 records to the server, and 2 fail .. do you consider that a success or fail? ”


is really the heart of my question. That result cannot be described as a total success or a fail. It is ExtJS that wants it described in binary terms which is what I find hard to understand. It is a partial success and a partial fail. It is this partial result that I want to respond to in a manner that is best practice for ExtJS.


For the example you mention, I would want the local Store to have one record updated and the other (failed) records removed. How can I express that outcome in the result sent back by the server?


Thanks
Jonathan

MikeRH
22 Mar 2014, 9:35 PM
It's up to you to program the server code to send back any failure back to Ext and then you need to cod Ext to handle the response. In your example if you send 3 records to the server and only 1 succeeded, your server code would then need to respond with a message containing the 2 failed records and your Ext code would need to mark the failed records in the store as dirty and alert the user.

JonathanStone
24 Mar 2014, 12:43 AM
Hi Scott

Thanks for your thoughts. I think I have clearer picture now and I would really appreciate you thoughts on whether my approach to partial success/failure is reasonable:

Scenario: AJAX Proxy, JSON Reader, Store autoSync = false

When I create a record(s), the server must assign an Id to each new record, hence if some of the records can be created they must be returned by the server with correct ids assigned.

For example, the following result would be sent by my server is some of the records were created successfully and some were not:

{
success: true,
records: [// an array of successfully created records with their server assigned ids],
message: 'Some records were not created'
}

When the Store.sync call is invoked a success and a failure callback are provided. When the success callback method is invoked, I can determine the records that were not created using the batch and options parameters. I can then remove the records from the store that were not successfully created.


Is the a reasonable strategy for dealing with partial success ?

Thanks

Jonathan

hajix
19 Sep 2014, 12:02 PM
Hi,

The answer makes perfect sense. My only question is about this portion:



In your example if you send 3 records to the server and only 1 succeeded, your server code would then need to respond with a message containing the 2 failed records and your Ext code would need to mark the failed records in the store as dirty and alert the user.


When you say "your Ext code", which part of the code are we talking about? Is anything needed to be done in the store, in the proxy, or somewhere else? Or is there any method that needs to be overwritten? A concrete class or method name will be highly appreciated.

Thanks