In the previous chapter, we performed CRUD operation on a single record. Here, we will learn how to perform CRUD operation on multiple records in a single request using sync() method i.e. create multiple records in a single request or update multiple records in a single request or delete multiple records in a single request.
Note: Please make sure that your service should accept an array of model objects as an input parameter because sync method will send an array of model objects as a request payload to the configured url.
We can call sync() method in the same way as seen in the previous chapter. However, we need to be careful in exception handling as shown in the following example:
//fires create, update and delete request when calling sync and commit changes in the store when autoSync=false studentStore.sync({ success : function(batch, opt){ Ext.Msg.alert('Status', 'Changes saved successfully.'); }, failure : function(batch, opt){ var msg = ''; if(batch.hasException){ for(var i = 0; i < batch.exceptions.length; i ++ ){ switch(batch.exceptions[i].action){ case "destroy" : msg = msg + batch.exceptions[i].records.length + " Delete, "; break; case "update" : msg = msg + batch.exceptions[i].records.length + " Update, "; break; case "create" : msg = msg + batch.exceptions[i].records.length + " Create, "; break; } } Ext.Msg.alert("Status", msg + " operation failed!"); } else Ext.Msg.alert('Status', 'Changes failed.'); } });
As per the above example, we check if any operation has failed in a batch and display number of failed create, update and delete operations.