Read data locally, update to server

11 posts, 1 answers
  1. Bilal
    Bilal avatar
    109 posts
    Member since:
    Oct 2014

    Posted 23 Mar 2015 Link to this post

    Hi,
    I am trying to work on a sample whereby I read data locally into the DataSource but I submit changes to server.

    Here is a dojo, however, an exception occurs when a call for dataSource.sync() is executed.

    Any idea why that is happening?

    http://dojo.telerik.com/@bhaidar/AYALi


    Thanks
  2. Alexander Popov
    Admin
    Alexander Popov avatar
    1416 posts

    Posted 25 Mar 2015 Link to this post

    Hi Bilal,

    The error occurs because mixing functions and objects for the transport operations is not supported. Basically, each operation should be of the same type as the "read", which in this case is a function.

    Regards,
    Alexander Popov
    Telerik
     
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
     
  3. Kendo UI is VS 2017 Ready
  4. Bilal
    Bilal avatar
    109 posts
    Member since:
    Oct 2014

    Posted 25 Mar 2015 in reply to Alexander Popov Link to this post

    Is there a workaround? I already have the data locally, however, I want to update on the server any changes that occurred on the client.

    Thanks
  5. Alexander Popov
    Admin
    Alexander Popov avatar
    1416 posts

    Posted 27 Mar 2015 Link to this post

    Hello Bilal,

    Sure, the workaround is to use a function for the Update as well and make and Ajax request that sends the data to the server. You can find an example in the transport.update documentation.

    Regards,
    Alexander Popov
    Telerik
     
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
     
  6. Bilal
    Bilal avatar
    109 posts
    Member since:
    Oct 2014

    Posted 28 Mar 2015 in reply to Alexander Popov Link to this post

    Hi,
    I am trying to send extra data to server with a create/update custom function.

    Any idea on how to do so? I want to send a single key/value. 

    Thanks
  7. Bilal
    Bilal avatar
    109 posts
    Member since:
    Oct 2014

    Posted 29 Mar 2015 in reply to Bilal Link to this post

    Hello,
    I managed to send extra data. However, I would like to share with you a behavior I am facing with the below implementation:
    stateMap.projectStaffDs = new kendo.data.DataSource({
          transport: {
              read: function(options) {
                  options.success(stateMap.projectStaff);
              },
              create: function (options) {               
                  $.ajax({
                      cache: false,
                      type: "POST",
                      url: document.getBaseUrl() + "/Services/SaveStaff",
                      contentType: "application/json; charset=utf-8",
                      dataType: "json",
                      data: JSON.stringify({ data: options.data.models, workspace: stateMap.userWorkspace.workspaceName }),
                      success: function (result) {
                          if (result.errors) {
                              options.error(result);
                          } else if (result.staff) {
                              options.success(result.staff);
                          }
                      },
                      error: function (result) {
                          options.error(result);
                      }
                  });
              
          },
          schema: {
              model: projectStaffModel
          },
          batch: true,
          pageSize: 1000
      });

    What I realized, as long as the response has code 200 OK, the success function of $.ajax is the only function called.
    In my case, I am returning errors when creating/updating data on server. However, I am returning 200 OK and adding errors as a property on the response data.

    So for this, I had always to work with success function and check if errors are present, if so, then I call options.error(results);

    The error function of $.ajax is never used. Are there cases it will be used?

    Moreover, when options.error is called, the error function defined on DataSource is fired however, the input for that function doesn't contain the data I sent when I called options.error(result). Why is that? Shouldn't the data I passed in to options.error be passed in to the DataSource error function?

    Moreover, I realized that when options.error() is called, the Grid doesn't cancelChanges, all changes done by user are kept the same.

    Also, I tried to implement the DataSource error function, whereby I bind one time to Grid using jquery .one, however, it was never fired, but fired when I click on cancelChanges on Grid.

    In general, is the above reliable to work with? In my case, I am loading data outside DataSource and hence the need to use this method.

    Thank you


  8. Daniel
    Admin
    Daniel avatar
    2117 posts

    Posted 01 Apr 2015 Link to this post

    Hello,

    When will the error callback be triggered is described in the jQuery ajax method documentation. 
    As for passing the errors to the error event  - you should set the field in the response that holds the errors with the schema.errors option and call the success callback:
    success: function (result) {
      if (result.errors) {
          options.success(result);
      } else if (result.staff) {
          options.success(result.staff);
      }
    }
    schema: {
        model: projectStaffModel,
        errors: "errors"
    }


    Regards,
    Daniel
    Telerik
     
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
     
  9. Bilal
    Bilal avatar
    109 posts
    Member since:
    Oct 2014

    Posted 01 Apr 2015 in reply to Daniel Link to this post

    Hi Daniel,

    I added that field. I was able to access the errors collections as this:

    args.xhr.errors

    There was no args.errors 

    Is there something wrong still?

    Thanks
  10. Daniel
    Admin
    Daniel avatar
    2117 posts

    Posted 03 Apr 2015 Link to this post

    Hello,

    Did you trigger the success or the error callback? The errors will be set to the xhr field if you have triggered the error callback.

    Regards,
    Daniel
    Telerik
     
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
     
  11. Bilal
    Bilal avatar
    109 posts
    Member since:
    Oct 2014

    Posted 03 Apr 2015 in reply to Daniel Link to this post

    I was reviewing your post, you used options.success for both cases? Even when there are errors, you are calling success method? Why is that? Shouldn't i call options.error?

     

    Thanks

  12. Answer
    Daniel
    Admin
    Daniel avatar
    2117 posts

    Posted 07 Apr 2015 Link to this post

    Hi,

    The error callback signifies that the request has failed which is not the case when using custom errors.

    Regards,
    Daniel
    Telerik
     
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
     
Back to Top
Kendo UI is VS 2017 Ready