迁移 Ext JS 4 代码库时无法覆盖 Ext.data.proxy.Ajax 实例上的方法 afterRequest
Cannot override method afterRequest on Ext.data.proxy.Ajax instance when migrating an Ext JS 4 codebase
背景:我希望能够为 ajax 加载错误创建自定义错误掩码,例如。以防网络问题。
我正在使用以下代码创建一个新的 Ext JS 存储,我想用我自己的 afterRequest
函数扩展其代理。
afterRequest
函数应该发送一个 afterload
事件,该事件由在小部件上调用 mask()
的实用函数使用。
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 是一个空函数
来自文档 -
This is a template method. a hook into the functionality of this
class. Feel free to override it in child classes.
来自源代码 (5.0)
/**
* Optional callback function which can be used to clean up after a request has been completed.
* @param {Ext.data.Request} request The Request object
* @param {Boolean} success True if the request was successful
* @protected
* @template
* @method
*/
**afterRequest: Ext.emptyFn,**
要覆盖此函数,您必须扩展“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 中 - 在配置 class 的实例期间,通过配置函数内部的配置器,对函数覆盖进行严格检查,如果您覆盖了函数,则会抛出异常在实例的配置中。
从配置函数内部抛出异常的 class 中提取(在重新配置函数内部还有另一个检查)-
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 是最安全的,但不推荐,因为它是私有标志。
注意:很多更改都没有真正记录下来,最好在遇到此类问题时查看源代码。
根据@yellen 的发现,我可以看出从 5.1 版开始,添加了严格模式检查以限制函数覆盖。在这种情况下,我认为这可能是一个错误,因为这是一个旨在被覆盖的空回调方法。无论如何,
可以通过将 $configStrict: false
添加到代理配置对象来通过此检查。可能他们只是明确声明了这种覆盖。
这就是您的代码的样子:
var settingsStore = new Ext.data.Store({
model: 'vmSettings',
autoload: 'true',
proxy: {
$configStrict: false,
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',
},
}
});
有关 $configStrict 的更多信息 here。
找工作 fiddle here.
背景:我希望能够为 ajax 加载错误创建自定义错误掩码,例如。以防网络问题。
我正在使用以下代码创建一个新的 Ext JS 存储,我想用我自己的 afterRequest
函数扩展其代理。
afterRequest
函数应该发送一个 afterload
事件,该事件由在小部件上调用 mask()
的实用函数使用。
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 是一个空函数
来自文档 -
This is a template method. a hook into the functionality of this class. Feel free to override it in child classes.
来自源代码 (5.0)
/** * Optional callback function which can be used to clean up after a request has been completed. * @param {Ext.data.Request} request The Request object * @param {Boolean} success True if the request was successful * @protected * @template * @method */
**afterRequest: Ext.emptyFn,**
要覆盖此函数,您必须扩展“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 中 - 在配置 class 的实例期间,通过配置函数内部的配置器,对函数覆盖进行严格检查,如果您覆盖了函数,则会抛出异常在实例的配置中。
从配置函数内部抛出异常的 class 中提取(在重新配置函数内部还有另一个检查)-
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 是最安全的,但不推荐,因为它是私有标志。
注意:很多更改都没有真正记录下来,最好在遇到此类问题时查看源代码。
根据@yellen 的发现,我可以看出从 5.1 版开始,添加了严格模式检查以限制函数覆盖。在这种情况下,我认为这可能是一个错误,因为这是一个旨在被覆盖的空回调方法。无论如何,
可以通过将 $configStrict: false
添加到代理配置对象来通过此检查。可能他们只是明确声明了这种覆盖。
这就是您的代码的样子:
var settingsStore = new Ext.data.Store({
model: 'vmSettings',
autoload: 'true',
proxy: {
$configStrict: false,
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',
},
}
});
有关 $configStrict 的更多信息 here。
找工作 fiddle here.