sencha touch override ext.ajax

我是研究僧i 提交于 2019-12-07 08:11:26

Finally, this is work with me

Ext.define('MyApp.Ajax', {    
    extend: 'Ext.data.Connection',

    singleton: true,

    request: function( options ) {
        options.headers = {
            Token: 'mytoken',
        };
        this.callParent( [options] );
    }
});

And this simple do what i want too, great.

Ext.Ajax.on('beforerequest', (function(conn, options, eOpts) {  
    options.headers = {
        Token: 'mytoken',
    };
}), this);

You don't seem to agree with yourself about the name of your override class:

  • Myapp.override.Ajax
  • Lunex.override.Ajax
  • Myapp.override.data.proxy.Ajax

Which one is it? Pay attention to this, the Loader won't go easy about it...

Anyway, you seem a bit confused about override and extend.

extend does create a new class from the parent class, with the modifications you've specified. Then you can use the class you defined, like you're trying to do.

override, on the other hand, applies the modification to the existing class. In this case, the class name is only used for the require statement, and by the loader. No actual class is created. So, in your example, the namespace MyApp.override is not defined, hence the error. Nevertheless, whenever you use Ext.Ajax, your custom header should be sent. Provided you're manager to load your file, that is ;p

Now, your case is a bit special because Ext.Ajax is a singleton instance of Ext.data.Connection. See its code, there's not much in there. And while overriding a singleton can make sense, extending from it would be disturbing.

So, what you were probably trying to do is that:

Ext.define('Myapp.Ajax', {
    extend: 'Ext.data.Connection',
    headers: {
        'token': 'test'
    }
});

So that you can do:

Myapp.Ajax.request({ ... });

Whether the best choice here is to override or to extend is a tough question. I'm glad you didn't ask it!

Why not using the config 'defaultHeaders' in your extended class? In that way it's always added to your headers.

http://docs-origin.sencha.com/touch/2.4.0/apidocs/#!/api/Ext.data.Connection-cfg-defaultHeaders

From the source of Ext.data.Connection

setupHeaders: function(xhr, options, data, params) {
    var me = this,
        headers = Ext.apply({}, options.headers || {}, me.getDefaultHeaders() || {}),
        contentType = me.getDefaultPostHeader(),
        jsonData = options.jsonData,
        xmlData = options.xmlData,
        key,
        header;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!