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

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

1 Answer 102 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
Artie
Top achievements
Rank 1
Artie asked on 11 Aug 2015, 04:05 PM

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:

http://dojo.telerik.com/uYaYI/4

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?

1 Answer, 1 is accepted

Sort by
0
Nikolay Rusev
Telerik team
answered on 13 Aug 2015, 09:05 AM

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.

 

http://dojo.telerik.com/@rusev/EXaPI

 

Regards,
Nikolay Rusev
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
Tags
Data Source
Asked by
Artie
Top achievements
Rank 1
Answers by
Nikolay Rusev
Telerik team
Share this question
or