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

how to retive data from server before save

2 Answers 45 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jatupoom
Top achievements
Rank 1
Jatupoom asked on 20 Nov 2018, 08:55 AM

i want to check condition before save   but still not work what im wrong ?

how to solve it

<p>save: Function(e) {</p><p>var currenVar = e.model.id;</p><p>var data;;</p><p>https.get($scope.service URL + currenVar.success(function (result){</p><p>data= result;</p><p>for (item in data){</p><p>if(data[item].id == currenVar  {</p><p>alert('Cannot use this');</p><p>e.preventdefault();</p><p>}</p><p>}</p>

2 Answers, 1 is accepted

Sort by
0
Jatupoom
Top achievements
Rank 1
answered on 20 Nov 2018, 09:03 AM
still show alert and save what im wrong with this ?
 
save: function(e)
 
var currenID =  e.model.id
$https.get($scrope.servceurl +currenID ).success(function(result){
var data = result;
 
for (item in data ) {
if(data[item].id == currenID )
{  alert(cannot use) ; 
  e.preventDefault();
 }
 
}
});
0
Tsvetina
Telerik team
answered on 22 Nov 2018, 07:41 AM
Hello Jatupoom,

This happens because the request to the server is asynchronous and there is no logic that would stop the save event from completing. The save completes before the response from the server is actually received. What you can do is:
1. Cancel the default save action.
2. Make the $http.get request.
3. If the conditions are met, manually trigger a DataSource sync.

Something like this:
save: function(e)
    var currenID =  e.model.id;
    e.preventDefault();
    $https.get($scrope.servceurl +currenID ).success(function(result){
        var data = result;
  
        for (item in data ) {
            if(data[item].id == currenID ){ 
                 alert(cannot use) ;
                 return; // exit loop and handler
            }
        }
        e.sender.dataSource.sync(); // complete save to server if logic reaches this point
    });
}


Regards,
Tsvetina
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
Grid
Asked by
Jatupoom
Top achievements
Rank 1
Answers by
Jatupoom
Top achievements
Rank 1
Tsvetina
Telerik team
Share this question
or