Update Data Method being called too early

5 posts, 0 answers
  1. Dan
    Dan avatar
    24 posts
    Member since:
    Jun 2020

    Posted 07 Jan Link to this post

    I have a grid that has the following code as the update action in its datasource.

    This is being called before the grid is rendered so I get an error that the datasource is null. Meaning I dont think the grid has initialized on the front end so the '#Grid' isnt there for the returnDirtyFields method to get. Why is this update action data method being run when the grid is not updating?

    The .Data() method I want with the update action below it.

    function returnDirtyFields(gridId) {
          var id = '#' + gridId;
          var dataSource;
          var data;
          var changedModels;
     
          dataSource = $(id).data("kendoGrid").dataSource;
          data = dataSource.data();
     
              if (dataSource.hasChanges()) {
              for (var i = 0; i < data.length; i++) {
     
                  if (data[i].dirty) {
                      changedModels = data[i].dirtyFields
                  }
                  }
               }
     
          var temp = $.extend(true, {}, { dirtyFields: JSON.stringify(changedModels) });
          return temp;
      }

     

    .Update(update => update.Action("Entry_Update","Entry").Data("returnDirtyFields('Grid')"))
  2. Anton Mironov
    Admin
    Anton Mironov avatar
    168 posts

    Posted 12 Jan Link to this post

    Hi Dan,

    Thank you for the provided code snippets and details.

    You are totally correct. The reason for the faulty behavior is that when the Kendo UI Grid is initializing, the Update method in the DataSource is finding the "returnDirtyFields" function. This function is IIFE(Immediately Invoked Function Expression) and executes at the same moment. The DataSource is still not available at this moment and an exception is thrown.

    In this case, I would recommend using Batch editing. This functionality provides the opportunity to keep all the changes(dirty fields) at the client(in the browser) and send them to the Update method when you decide.

    Here is a demo that represents the implementation and behavior of the Batch editing:

    Give the approach above a try and let me know if further assistance is needed.

     

    Kind Regards,
    Anton Mironov
    Progress Telerik

    Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

  3. Dan
    Dan avatar
    24 posts
    Member since:
    Jun 2020

    Posted 13 Jan in reply to Anton Mironov Link to this post

    Anton,

    Thank you for the response. I did not try to implement batch editing. Instead I decided to opt for logging (which is what my question context was) the whole object instead of just dirty values. Thank you for the response anyway!

  4. Anton Mironov
    Admin
    Anton Mironov avatar
    168 posts

    Posted 13 Jan Link to this post

    Hi Dan,

    The solution you had chosen is a good one. Is this achieved all the needed requirements for the application??

    Let me know if I could assist you with anything else, or this thread should be closed.

     

    Best Regards,
    Anton Mironov
    Progress Telerik

    Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

  5. Dan
    Dan avatar
    24 posts
    Member since:
    Jun 2020

    Posted 13 Jan in reply to Anton Mironov Link to this post

    Anton,

    Thank you. Yes you can close this thread. 

Back to Top