How to revert changes on an item added to a local DataSource

2 posts, 0 answers
  1. Artie
    Artie avatar
    27 posts
    Member since:
    May 2015

    Posted 11 Aug 2015 Link to this post

    Consider the following scenario:

    1. Local dataSource is populated with .data()
    2. A new data item is added to the dataSource using .add()
    3. .cancelChanges() is called on the dataSource, item in #2 disappears

    I tried inserting a .sync() call after item #2, but I found that .cancelChanges() will always remove newly added items to the DataSource that were added using .add().  After doing some additional research, I'm beginning to question exactly how local DataSource's are supposed to be used.  I've seen a lot of mention of having to use the four transport methods, and that the changes themselves should actually be persisted to a Javascript array, and after a .add(), the array should be updated and the .data() method should be called again.  How exactly should this be implemented?

    I've put together a Dojo that illustrates the behavior from which I will base these questions:

    In the initialization block, if I add each of my initial 25 items via .add(), then click on any one of them and hit "Cancel" then they are actually removed from the data source.  If I instead put my initial items in an array and call .data(), then the .cancelChanges seems to work as expected.  However, if I use the .add() method, then "Save," then try to hit it and then hit "Cancel" again I see that the item disappears.  When doing CRUD operations on a local dataSource, is it required that the data be persisted to a Javascript array?  For clarity, could someone explain why this is?

  2. Nikolay Rusev
    Nikolay Rusev avatar
    2275 posts

    Posted 13 Aug 2015 Link to this post

    Hello Artie,


    In case of local editing you will need to define transport. Also in order to distinguish new items from existing the DataSource is using the id field, ID in your case, of the model. Thus you will need to define it in the model fields with a default value. I've set default value to -1 as you've already have item with ID=0 and it should not be treated as new.


    I've also simplified the cancel, addNew and save functions. Bellow you will find the updated example.



    Nikolay Rusev
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Back to Top