本文介绍了灰烬 - 中止Ajax请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我想知道如何中止请求。

I want to know how to abort a request.

例如,我发出了 App.MyModel.find()后来我想取消/放弃之前从服务器返回。我希望是这样的:

For example, I issue App.MyModel.find() and later i want to cancel/abort it before it returns from server. I would expect something like this:

var res = App.MyModel.find();   //request sent to server
res.abort();                    // abort the request before completion

但这不起作用 - 返回的对象是一个键,既无中止取消的方法。

but this doesn't work - the returned object is a promise and there is neither abort nor cancel methods.

我在寻找如何调用的底层 XMLHtt prequest 对象的方法。

I'm looking for how to call the abort method on the underlying XMLHttpRequest object.

推荐答案

我相信,当你调用 App.MyModel.find(),它会返回一个 RecordArray ,不是

I believe, when you call App.MyModel.find(), it's going to return you a RecordArray, not a promise.

从看余烬数据源$ C ​​$下RESTAdapter,我不认为有一种方法叫放弃对你的 XMLHtt prequest 对象

From looking at the ember-data source code for RESTAdapter, I don't think there's a way to call abort on your XMLHttpRequest object.

您可以实现自己的RESTAdapter:

You can implement your own RESTAdapter:

var myRESTAdapter = DS.Adapter.create({
  find: function (store, type, id) {
    var req = $.ajax({
      url:      type.url,
      dataType: 'jsonp',
      context:  store,
      success:  function(response){
        this.load(type, id, response.data);
      }
    });
    req.abort();
  },
  findAll: function(store, type) {
    var req = $.ajax({
      url:      type.url,
      dataType: 'jsonp',
      context:  store,
      success:  function(response){
        this.loadMany(type, response.data);
      }
    });
    req.abort();
  }
});

和呼叫中止在 XMLHtt prequest 对象。

这篇关于灰烬 - 中止Ajax请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 21:48