将ajax调用封装在延迟对象中并允许中止

时间:2015-05-04 17:09:24

标签: jquery jquery-deferred

我正在尝试创建一个负责某些全局功能的全局ajax调用,例如:

JSON

{
  Data : { /* some object returned */ },
  RedirectTo: "",   // for example: "/Account/Login"
  ErrorMessage: "", // for example: "You have been logged out do to inactivity.
}

jquery的

(function (global, $, undefined)
{
  global = global || {};

  global.ajax = function(settings)
  {
    var timeoutResult = {
      Data: null,
      RedirectTo: null,
      ErrorMessage: "Your request could not be completed, please try again.";
    }

    var customResult;
    var jqXHR;
    var $deferred = $.Deferred(function()
    {
      var ajaxSettings = $.extend({
          statusCode: {
            408: function()
            {
              customResult = timeoutResult; 
            },
          },
        }, settings);


      var self = this;
      jqXHR = $.ajax(ajaxSettings )
        .always(function(jsonResult)
        {
          if (customResult) jsonResult = customResult;
          if (jsonResult.RedirectTo
              && jsonResult.RedirecTo !== null)
          {
            window.location = jsonResult.RedirectTo;
          }
        })
        .fail(function(jsonResult)
        {
          if (customResult) jsonResult = customResult;
          if (/* some logic to bubble up fail() */)
          {
            self.reject(jsonResult);
          }
        })
        .done(function(jsonResult)
        {
          if (customResult) jsonResult = customResult;
          if (/* some logic to bubble up done() */)
          {
            self.resolve(jsonResult);
          }
        });
    };

    return ???;

  };
}(window.global, jQuery));    

现在的问题是,我希望用户能够.abort().done().fail(),但我不希望他们将延迟呼叫直接附加到{ {1}}但如果我不这样做,他们就无法拨打jqXHR

我可以返回一个像这样的对象:

abort()

但它不直观。

  

原因?

我需要控制哪些{ jqXHR: jqXHR, deferred: self } done()事件/方法会影响开发人员代码。如果用户已注销,我将立即重定向。其他开发人员代码不需要执行,也不应该真正执行。

  

请求如何中止?

开发人员希望能够:

fail()

无论什么原因。

1 个答案:

答案 0 :(得分:0)

我应该在Javascript世界中知道我可以根据需要扩展对象:

global.ajax = function(settings)
  {
    var timeoutResult = {
      Data: null,
      RedirectTo: null,
      ErrorMessage: "Your request could not be completed, please try again.";
    }

    var customResult;
    var jqXHR;
    var $deferred = $.Deferred(function()
    {
      var self = this;

      var ajaxSettings = $.extend({
          statusCode: {
            408: function()
            {
              customResult = timeoutResult; 
            },
          },
        }, settings);


      jqXHR = $.ajax(ajaxSettings )
        .always(function(jsonResult)
        {
          if (customResult) jsonResult = customResult;
          if (jsonResult.RedirectTo
              && jsonResult.RedirecTo !== null)
          {
            window.location = jsonResult.RedirectTo;
          }
        })
        .fail(function(jsonResult)
        {
          if (customResult) jsonResult = customResult;
          if (/* some logic to bubble up fail() */)
          {
            self.reject(jsonResult);
          }
        })
        .done(function(jsonResult)
        {
          if (customResult) jsonResult = customResult;
          if (/* some logic to bubble up done() */)
          {
            self.resolve(jsonResult);
          }
        });

      // add the abort method... 
      self.abort = jqXHR.abort;

    };
  };

return $deferred;