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

If ID is provided in add(), sync does not update _pristineData and Create/Update are not invoked.

4 Answers 588 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
Andy
Top achievements
Rank 1
Andy asked on 05 May 2015, 05:49 PM

I'm working with local data and I began this by having an issue where hitting cancel on the edit popup removed a row that I added using the DataSource.add() method as described in the api for the add method (http://docs.telerik.com/kendo-ui/api/javascript/data/datasource#methods-add), and with the sync method (http://docs.telerik.com/kendo-ui/api/javascript/data/datasource#methods-sync).  

After much investigation, I found that if I provided the ID defined in the model as part of the add() method and followed with a sync() method call, that the _pristineData property was not being updated properly.  

See http://dojo.telerik.com/@aw232/OZuKA/2 for an example I created to illustrate what I mean.  Clicking the “Click Me” button calls add(), including a value for the id as defined in the model ("ProductID"), followed by a sync() method call.  The product appears to be successfully added to the grid, but clicking on edit and then cancel removes the row.  This is because the _pristineData property is not updated after the sync(), so the DataSource reverts back to that data when the cancel event is called.  Additionally, neither the Create nor Update Transports are being called.  

However, if I don’t provide the ID, as defined in the model, the Create Transport function is called, and the _pristineData is being successfully updated when I called the sync() method.  See http://dojo.telerik.com/@aw232/uKIji/2 for an example.  The code here is identical to the first snippet, except that the model now uses “ID” as the id, which is not provided in the add() method.  As you can see, the create transport function is successfully called and _pristineData is updated and thus, clicking cancel does not remove the row from the datasource.  

I would expect that sync() would push the current data to _pristineData either directly, or by calling the Create or Update transports as necessary.   

Please advise.  


4 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 07 May 2015, 03:30 PM
Hello Andy,

The behavior is expected. Because the record has not default ID value it is considered as existing and not a new one so it will not be synchronized. The record ID field should be assigned only from the transport create operation.

Regards,
Daniel
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Andy
Top achievements
Rank 1
answered on 08 May 2015, 03:11 PM

[quote]The behavior is expected. Because the record has not default ID value it is considered as existing and not a new one so it will not be synchronized. The record ID field should be assigned only from the transport create operation.[/quote]

 Daniel,  

I appreciate the response, but this behavior is NOT expected.  If the record isn't considered new, then the Update transport would be expected to be called as part of the sync() method, but it is not.  Instead, the record is only ever temporarily added and there is no way to persist the added record into the _pristineData.  Worse, the temporary record is displayed using the grid control which leads to confusion when it mysteriously disappears after cancelling an edit.  

 If this is the expected and desired behavior, and adding a record with the id is not allowed, then an error should be thrown.  Either disallow the behavior completely, or allow it to function as in other cases.  Allowing the add()/sync() to appear successful and showing the record in the grid is simply not helpful.  

One of the following behaviors would be expected in this case:

1.  Anytime the add() method is used, it should be considered a 'new' record, or at least when the id provided does not match with any other id currently in the data array. If it matches a record in the data array, run the Update transport, otherwise, run the Create transport.  Simply including a value for the id should not remove the record from ever being persisted. 

2. Using the sync() method should force what is current in the data property to be put in _pristineData rather than making the developer believe that the record was successfully added when it was not..

3.  An error should be thrown when the developer includes the id for the model in the object passed into add().  

Thanks,

 Andy

 

0
Daniel
Telerik team
answered on 12 May 2015, 11:26 AM
Hello again Andy,

The update method will not be called because the record is not modified.
Regarding your suggestions:
  1. The current logic is to check for the ID field value and is described in the documentation. If you believe that it should be changed then I can suggest to open a request in our user voice forum.
  2. The pristine data should be updated only when the records are synchronized with the server. Otherwise, it will not be correct if the server is not able to save the changes and returns an error response.
  3. This will be a breaking change and will prevent adding records in all cases that do not require synchronizing or cancelling the changes via the dataSource.

Regards,
Daniel
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Andy
Top achievements
Rank 1
answered on 18 May 2015, 07:25 PM

Daniel, 

 I've done as you suggested and added it to the user voice forum:  http://kendoui-feedback.telerik.com/forums/127393-telerik-kendo-ui-feedback/suggestions/8017020-on-datasource-if-id-is-provided-in-add-sync-do.  

 Thanks, 

Andy

Tags
Data Source
Asked by
Andy
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Andy
Top achievements
Rank 1
Share this question
or