简体   繁体   English

Kendo DataSource:如何取消更新请求

[英]Kendo DataSource: how cancel an update request

I have a kendo UI grid and a datasource. 我有一个kendo UI网格和一个数据源。 When I call the Update method, I test a variable and if the condition is false, I don't want to send the request. 当我调用Update方法时,我测试一个变量,如果条件为false,我不想发送请求。

Currently I have: 目前我有:

 $scope.MySource = new kendo.data.DataSource({
  update: {
            url: function (lista) {
                if (testVariable== true) {           
                    testVariable= false;
                    return "api/Liste/PutLista/" + lista.Id
                }
                else {
                    $scope.MySource.cancelChanges();
                }
            },
            type: "PUT",
            dataType: "json",
            beforeSend: function (req) {
                var auth = $window.sessionStorage.token;
                req.setRequestHeader('Authorization', 'Bearer ' + auth);
            }

If testVariable is false I cancel the update but I still see an ajax request to 如果testVariable为false,我取消更新,但仍然看到ajax请求

http://localhost:61927/index.html with PUT method.

How can I prevent a request if testVariable is false? 如果testVariable为false,我该如何阻止请求?

You can intercept it right after you click on update. 您可以在单击更新后立即拦截它。 In your kendo grid, use the save event. 在您的kendo网格中,使用save事件。 This event is called every time you press update. 每次按更新时都会调用此事件。

save: function (e) {
    if (testVariable === false) {           
        e.preventDefault();         //prevent default action that kendo does
        $scope.MySource.cancelChanges();
    }
}

If the variable is false, then you prevent the default action and do whatever you want (like cancel changes). 如果变量为false,则可以阻止默认操作并执行任何操作(例如取消更改)。 If it is true, it will just take the default action and save your row. 如果是,它将采取默认操作并保存您的行。

Just make sure that the variable testVariable is accessible in this event (global variable, or a local variable that you can set in this event). 只需确保在此事件中可以访问变量testVariable (全局变量或您可以在此事件中设置的局部变量)。

The url function is not the place to check if you actually want to make the request. url函数不是检查您是否确实要发出请求的地方。 I'm pretty sure there is no way to cancel once it has made it that far. 我很确定一旦它取得了那么远就没有办法取消。

You may want to try using the requestStart event instead. 您可能希望尝试使用requestStart事件。 In some cases, returning false from the event handler will cancel the request (but not in all cases, which might be a kendo bug). 在某些情况下,从事件处理程序返回false将取消请求(但并非在所有情况下,这可能是一个kendo错误)。

Otherwise, you can just overwrite the sync() function on the DataSource instance, do your checks, then call the base implementation if needed. 否则,您只需覆盖DataSource实例上的sync()函数,执行检查,然后在需要时调用基本实现。

          $scope.MySource.sync = function () {
            if(customChecksPass) {
                kendo.data.DataSource.fn.sync.apply(this, arguments);
            }
          };

I also just re-read your question and realized you are using the Grid. 我也只是重新阅读你的问题,并意识到你正在使用网格。 The grid widget has a save event that you can bind to, and cancel the call to the DataSource from there. 网格小部件有一个可以绑定的保存事件 ,并从那里取消对DataSource的调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM