0

ExtJS4:我在将我的应用程序 ExtJs 版本从 3.4.0 升级到 4.1.1a 时遇到问题。

我的 3.4.0 版本代码:

    this.jsonStore = new Ext.data.JsonStore({
       proxy    : new Ext.data.HttpProxy({
        url: 'rs/environments',
        disableCaching: true
        }),
    restful : true,
    storeId : 'Environments',
    idProperty: 'env',
    fields  : [
         'ConnectionName', 'Type'
    ]
});

this.colmodel = new Ext.grid.ColumnModel({
    defaults: {
        align: 'center'
    },
    columns: [{
                    header: Accero.Locale.text.adminlogin.connectionsHeading,
                    width   : 140,
                    dataIndex: 'ConnectionName'
            },
            {
                    header: Accero.Locale.text.adminlogin.connectionTypeHeader,
                    width   : 120,
                    dataIndex: 'Type'
            }]
});

config = Ext.apply({
    enableHdMenu: false,
    border      : true,
    stripeRows  : true,
    store       : this.jsonStore,
    view        : new Ext.grid.GridView(),
    header      : false,
    colModel    : this.colmodel,
    sm          : new Ext.grid.RowSelectionModel({singleSelect: true}),
    loadMask: {
            msg: Accero.Locale.text.adminlogin.loadingmask
            }
}, config);

我进行了以下更改以使应用程序与 ExtJs4.1.1 一起工作:

var sm = new Ext.selection.CheckboxModel( { 
        listeners:{ 
            selectionchange: function(selectionModel, selected, options){ 
                // Must refresh the view after every selection 
                myGrid.getView().refresh(); 
                // other code for this listener 
            } 
        } 
    }); 

    var getSelectedSumFn = function(column){ 
        return function(){ 
            var records = myGrid.getSelectionModel().getSelection(), 
            result  = 0; 
            Ext.each(records, function(record){ 
                result += record.get(column) * 1; 
            }); 
            return result; 
        }; 
    } 


    var config = Ext.create('Ext.grid.Panel', { 
        autoScroll:true, 
        features: [{ 
            ftype: 'summary' 
        }], 
        store: this.jsonStore, 
        defaults: {               // defaults are applied to items, not the container 
            sortable:true 
        }, 
        selModel: sm, 
        columns: [ 
            {header: Accero.Locale.text.adminlogin.connectionsHeading, width: 140, dataIndex: 'ConnectionName'}, 
            {header: Accero.Locale.text.adminlogin.connectionTypeHeader, width: 120, dataIndex: 'Type'} 
        ], 
        loadMask: {
            msg: Accero.Locale.text.adminlogin.loadingmask
            },
        viewConfig: { 
            stripeRows: true 
        } 
    }, config);

通过这些更改,我在本地文件“ext-override.js”中收到错误消息,提示“this.el is not defined”。

调试代码发现,在当前对象this中,没有el对象。

ext-override.js 代码:

(function() {
    var originalInitValue = Ext.form.TextField.prototype.initValue;
    Ext.override(Ext.form.TextField, { 
            initValue: function() {
                originalInitValue.apply( this, arguments );
                if (!isNaN(this.maxLength) && (this.maxLength *1) > 0 && (this.maxLength != Number.MAX_VALUE)) {
                    this.el.dom.maxLength = this.maxLength *1;
                }
            }
        }
    );
})();

请建议我哪里出错了?

提前致谢...

4

1 回答 1

0

说真的,使用更多的惰性初始化!你的代码是一堆对象,都是非结构化的。

首先,您可以使用类似的方法更轻松地覆盖和使用被覆盖的方法(从 4.1 开始)

Ext.override('My.Override.for.TextField', {
  override : 'Ext.form.TextField',
  initValue: function() {
    this.callOverridden(arguments);
    if (!isNaN(this.maxLength) && (this.maxLength *1) > 0 && (this.maxLength != Number.MAX_VALUE)) {
      this.el.dom.maxLength = this.maxLength *1;
    }
  }
});

但是:方法initValueinitField(和 this 在initComponent)中被调用,因此您无法引用this.me因为组件实际上没有(完全)呈现。

所以,这应该有帮助(未经测试):

Ext.override('My.Override.for.TextField', {
  override : 'Ext.form.TextField',
  afterRender: function() {
    this.callOverridden(arguments);
    if (!isNaN(this.maxLength) && (this.maxLength *1) > 0 && (this.maxLength != Number.MAX_VALUE)) {
      this.el.dom.maxLength = this.maxLength *1;
    }
  }
});

但我强烈建议不要在覆盖中使用这些东西。制作可以提高代码可读性的专用组件。

于 2012-09-28T19:17:24.263 回答