本文介绍了迁移 Ext JS 4 代码库时无法覆盖 Ext.data.proxy.Ajax 实例上的 afterRequest 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:我希望能够为 ajax 加载错误创建自定义错误掩码,例如.以防网络问题.

Background: I want to be able to create a custom error mask for ajax loading errors, for ex. in case of network problems.

我使用以下代码创建一个新的 Ext JS 存储,我想用我自己的 afterRequest 函数扩展它的代理.afterRequest 函数应该发送一个 afterload 事件,该事件由在小部件上调用 mask() 的实用函数使用.

I am using the following code to create to create a a new Ext JS store, whose proxy I would like to extend with my own afterRequest function.The afterRequest function should send an afterload event, which is used by a utility function calling mask() on the widget.

var settingsStore = new Ext.data.Store({
  model: 'vmSettings',
  autoload: 'true',
  proxy: {
    type: 'ajax',
    url: 'ta-debian-7-wheezy-virtualbox.json',
    afterRequest: function(request, success) {
      this.fireEvent('afterload', this, request, success);
      return;
    },
    reader: {
      type: 'json',
      rootProperty: 'variables',
    },
  }

});

现在此代码正在加载 Ext JS 时出现错误,但在 5.1 上失败并显示消息:无法覆盖 Ext.data.proxy.Ajax 实例上的方法 afterRequest

Now this code is loading withour error with Ext JS but fails on 5.1 with the message: Cannot override method afterRequest on Ext.data.proxy.Ajax instance

这里有什么问题?

推荐答案

afterRequest 是一个空函数

来自文档 -

这是一个模板方法.一个钩子到这个功能班级.随意在子类中覆盖它.

来自源代码 (5.0)

From source code (5.0)

/*** 可选的回调函数,可用于在请求完成后进行清理.* @param {Ext.data.Request} request 请求对象* @param {Boolean} success 如果请求成功则为真* @保护* @模板* @方法*/

**afterRequest: Ext.emptyFn,**

要覆盖此功能,您必须扩展Ext.data.proxy.Ajax".在子类中给出一个函数定义,并在您的商店中使用该子类作为代理.FIDDLE

To override this function, you'll have to extend "Ext.data.proxy.Ajax". Give a function definition in the child class and use that child class in your store as the proxy. FIDDLE

为什么会出现异常?

在 Ext 5.1 - 在配置类的实例期间,通过配置函数内的配置器,对函数覆盖进行严格检查,如果您覆盖了实例配置中的函数,则会抛出异常.

In Ext 5.1 - During configuring an instance of a class, by the Configurator inside the configure function, a strict check happens for function override and the exception is thrown if the function you've overridden a function in the intance's config.

查找Ext.Configurator

从配置函数内部抛出异常的类中提取(在重新配置函数内部还有另一个检查) -

Extract from the class where the exception is thrown from inside the configure function (there's another check inside the reconfigure function as well) -

 if (instance.$configStrict && typeof instance.self.prototype[name] ===
 'function') {
                         // In strict mode you cannot override functions
                         Ext.Error.raise("Cannot override method " + name + " on " + instance.$className + " instance.");
                     }

将 $configStrict 从 true(默认)覆盖为 false 是最安全的,但不推荐,因为它是私有标志.

It is mostly safe to override $configStrict from true(default) to false, but not recommended as it is a private flag.

注意:许多更改并未真正记录在案,最好在遇到此类问题时查看源代码.

这篇关于迁移 Ext JS 4 代码库时无法覆盖 Ext.data.proxy.Ajax 实例上的 afterRequest 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!