This is a migrated thread and some comments may be shown as answers.

Prevent ClientDataSource item remove if sync fails

2 Answers 7 Views
ClientDataSource
This is a migrated thread and some comments may be shown as answers.
Omar
Top achievements
Rank 1
Omar asked on 28 Nov 2016, 05:40 AM

Hi, I'm using a ClientDataSource with a Grid to communicate with a webservice from client-side.

The webservice returns a custom response for any request: {"ErrorMessage":(error message or null),"IsSuccessful":(boolean),"Result":[(result of request)]}.

In the ClientDataSource I defined the schema as follows to check for server errors

<Schema DataName="Result" ResponseType="JSON" ErrorsName="ErrorMessage">
  <Model ID="id">
  </Model>
</Schema>

And this function to show the error:

function dataSource_error(e) {
   e.preventDefault();
   console.log("Error", e);
   serverNotification.set_title("Server Error");
   serverNotification.set_text(e.errors);
   serverNotification.update();
   serverNotification.show();
 }

My problem is that when I try to remove an item from the clientdatasource and I get a custom error,the item is still removed from the clientdatasource and the grid which is not an effect I'm looking for.

For the clientdatasource remove/destroy I'm using a function:

dataSource.transport.destroy = function (options) {
   console.log("Options-destroy", options);
   console.log(dataSource);
   $.ajax({
      url: ("<%= WebApp.WebServiceHelper.BASE_WS_URL + WebApp.WebServiceHelper.WS_CLOSE %>"),
      dataType: "json",
      data: {
         username: sessionStorage["username"],
      },
      success: function (result) {
        console.log("result-destroy:", result);
                        //if (result.IsSuccessful) {
        options.success(result);
                        //} else {
                        //    options.error(result);
                        //}
        console.log("Options-destroy-success", options);
        console.log(dataSource);
      },
      error: function (result) {
        console.log("result1-destroy:", result);
        options.error(result);
      }
   });
};

Before adding ErrorsName in the schema I was check the IsSuccessful on success and call options.error, in this case the clientDataSource remove the item but the grid did not rebind !

On the same topic is it possible to check if IsSuccessful is false instead of ErrorMessage is null to generate an error and get the error message from ErrorMessage?

In case of remove, the Result will be a boolean, can I also prevent the clientDataSource from removing an element if if Result is false?

 

2 Answers, 1 is accepted

Sort by
0
Omar
Top achievements
Rank 1
answered on 29 Nov 2016, 10:20 AM
Also similar thing occurs when updating an item, I loaded the page then disconnected my internet connection and tested update and remove. The error method is called but the item is either updated or removed.
0
Accepted
Marin
Telerik team
answered on 30 Nov 2016, 02:45 PM
Hello,

If an error occurs, you should rebind the grid (calling .rebind() method on the client-side). This should force the read method of the ClientDataSource to be executed again and the grid should display the correct records.

Regards,
Marin
Telerik by Progress
Telerik UI for ASP.NET AJAX is ready for Visual Studio 2017 RC! Learn more.
Tags
ClientDataSource
Asked by
Omar
Top achievements
Rank 1
Answers by
Omar
Top achievements
Rank 1
Marin
Telerik team
Share this question
or