如何扩展Ext.Ajax?

时间:2012-06-14 15:17:05

标签: extjs extjs4 extjs-mvc

我经常在我正在制作的应用程序中使用它,所以我想缩短它,如果我能做一个扩展类来处理一个地方的所有回调逻辑,我试着像这样设置它但是失败了当试图加载类:

Uncaught TypeError: Cannot read property 'isInstance' of undefined ext-all-debug.js:4136
ExtClass.registerPreprocessor.config ext-all-debug.js:4136
Ext.apply.doProcess ext-all-debug.js:4007
Manager.registerPostprocessor.uses ext-all-debug.js:6037
Ext.apply.require ext-all-debug.js:5771
Manager.registerPostprocessor.uses ext-all-debug.js:6004
Ext.apply.doProcess ext-all-debug.js:4007
Ext.apply.doProcess ext-all-debug.js:4008
Ext.apply.process ext-all-debug.js:3995
Ext.Class.ExtClass ext-all-debug.js:3911
Ext.ClassManager.create ext-all-debug.js:4676
Ext.apply.define ext-all-debug.js:5095
(anonymous function) Ajax.js:1 //My extended Ajax singleton

以下是我尝试调用它的方式

    var changes = e.record.getChanges();
    if (e.record.get('id'))
        changes.id = e.record.get('id');

    APP.ux.Ajax.request({
        url: '/admin/user/save/',
        params: changes,
        scope: this
    });

更新 这是有道理的,我不敢相信,但我仍然得到相同的读取属性'isInstance'上面的未定义错误。以下是它现在的样子

这是简单的类(已更新)

Ext.define('APP.ux.Ajax', {
extend: 'Ext.data.Connection',
requires: [
    'APP.ux.Msg'
],
singleton : true,
autoAbort : false,
request: function(config) {
    var cfg = config;

    Ext.apply(cfg, {
        success: function(form, action) {
            APP.ux.Msg.alert('Success', action.result.msg);
            //TODO: Add more logic here
        },
        failure: function(form, action) {
            switch (action.failureType) {
                case Ext.form.action.Action.CLIENT_INVALID:
                    APP.ux.Msg.alert('Failure', 'Form fields may not be submitted with invalid values');
                    break;
                case Ext.form.action.Action.CONNECT_FAILURE:
                    APP.ux.Msg.alert('Failure', 'Ajax communication failed');
                    break;
                case Ext.form.action.Action.SERVER_INVALID:
                    APP.ux.Msg.alert('Failure', action.result.msg);
                    break;
            }
        }
    });
    this.callParent(cfg);
}
});

1 个答案:

答案 0 :(得分:1)

这不会像这样工作。如果你想在一个地方有失败/成功处理程序 - 你需要覆盖标准的request()方法,就像这样(这将转到你的App.ux.Ajax类)

request: function(config) {
  var cfg = config;

  Ext.apply(cfg, {
    success: function(form, action) {
       APP.ux.Msg.alert('Success', action.result.msg);
       //TODO: Add more logic here
    },
    failure: function(form, action) {
       switch (action.failureType) {
        case Ext.form.action.Action.CLIENT_INVALID:
            APP.ux.Msg.alert('Failure', 'Form fields may not be submitted with invalid values');
            break;
        case Ext.form.action.Action.CONNECT_FAILURE:
            APP.ux.Msg.alert('Failure', 'Ajax communication failed');
            break;
        case Ext.form.action.Action.SERVER_INVALID:
            APP.ux.Msg.alert('Failure', action.result.msg);
            break;
       }
    }
  });

  this.callParent(cfg);
}