代码之家  ›  专栏  ›  技术社区  ›  KallDrexx

ext js布局扩展导致ext-all.js中出现错误

  •  0
  • KallDrexx  · 技术社区  · 14 年前

    ext-base.js ext-all.js 正确包含。我的js文件中有以下内容:

    Ext.BLANK_IMAGE_URL = '<%= Url.Content("~/Content/ext/images/default/s.gif") %>';
    Ext.ns('MyNamespace');
    
    Ext.onReady(function() {
        alert("onReady() fired");
    });
    

    到目前为止,一切正常,没有错误,警报被正确抛出。然后在onReady之后添加以下代码:

    MyNamespace.BaseLayout = Ext.Extend(Ext.Viewport({
        layout: 'border',
        items: [
            new Ext.BoxComponent({
                region: 'north',
                height: 32,
                autoEl: {
                    tag: 'div',
                    html: '<p>North</p>'
                }
            })
        ]
    }));
    

    Uncaught TypeError: Object #<an Object> has no method 'addEvents'       ext-all.js:7
    Ext.Component       ext-all.js:7
    Ext.apply.extend.K       ext-base.js:7
    Ext.apply.extend.K       ext-base.js:7
    Ext.apply.extend.K       ext-base.js:7
    (anonymous function)       MyApp.js:13 (pointing to the Ext.Extend line)
    

    如果我把Viewport代码直接放到OnReady函数中(如下所示)

    Ext.onReady(function () {
        var bl = new Ext.Viewport({
            layout: 'border',
            items: [
            new Ext.BoxComponent({
                region: 'north',
                height: 32,
                autoEl: {
                    tag: 'div',
                    html: '<p>North</p>'
                }
            })
        ]
        });
    });
    

    它起作用了。有人能告诉我扩展方法的错误吗?

    3 回复  |  直到 14 年前
        1
  •  1
  •   rwilliams    14 年前

    这里有一个更复杂的例子来说明你想要完成什么。我强烈建议你去看看Saki's 3 part series 在使用ExtJS构建大型应用程序时,它将帮助您了解如何正确使用extend来创建可重用组件。

    Ext.ns('MyNamespace');
    
    MyNamespace.BaseLayout = Ext.extend(Ext.Viewport, {
        initComponent:function() {
            var config = {
                layout: 'border',
                items: [
                    new Ext.BoxComponent({
                        region: 'north',
                        height: 32,
                        autoEl: {
                            tag: 'div',
                            html: '<p>North</p>'
                        }
                    })
                ]
            };
            Ext.apply(this, Ext.apply(this.initialConfig, config));
    
            MyNamespace.BaseLayout.superclass.initComponent.apply(this,arguments);
        }//end initComponent
    
    });
    
    //this will give you an xtype to call this component by.
    Ext.reg('baselayout',MyNamespace.BaseLayout);
    
    Ext.onReady(function() {
        new MyNamespace.BaseLayout({});
    });
    
        2
  •  2
  •   Brian Moeskau    14 年前

    MyNamespace.BaseLayout = Ext.Extend(Ext.Viewport, {
        layout: 'border',
        items: [
            new Ext.BoxComponent({
                region: 'north',
                height: 32,
                autoEl: {
                    tag: 'div',
                    html: '<p>North</p>'
                }
            })
        ]
    });
    

    不过,我建议你采纳@r-dub的建议,多读一些你想做的事情。

        3
  •  0
  •   Kremena Lalova AndreKR    10 年前

    ExtJS建议使用define而不是extend。下面是一个类似的例子如何与define一起工作:

     Ext.define('Grid', {
        extend: 'Ext.grid.Panel',
        config: {          
            height: 2000        
        },
    
        applyHeight: function (height) {           
                return height;
        }       
    });
    
    new Grid({
        store: store,
        columns: [{
            text: 'Department',
            dataIndex: 'DepartmentName',
            renderer: function (val, meta, record) {
                return '<a href="DepartmentEmployees/' + record.data.ID + '">' + record.data.DepartmentName + '</a>';
            },
            width: 440,
            flex: 1,
            filter: 'string',
            sortable: true,
            hideable: false
        }, {
            text: 'Department Code',
            dataIndex: 'DepartmentKey',
            width: 100,
            flex: 1,
            filter: 'string',
            sortable: true,
            hideable: false
        }, {
            text: 'Main Phone',
            dataIndex: 'MainPhone',
            flex: 1,
            filter: 'string',
            sortable: true,
            hideable: false
        }, {
            text: 'Room',
            dataIndex: 'RoomLocation',
            flex: 1,
            filter: 'string',
            sortable: true,
            hideable: false
        }, {
            text: 'Hideway Location',
            dataIndex: 'HideawayLocation',
            flex: 1,
            filter: 'string',
            sortable: true,
            hideable: false
        }, {
            text: 'Hideway Phone',
            dataIndex: 'HideawayPhone',
            flex: 1,
            filter: 'string',
            sortable: true,
            hideable: false
        }, {
            text: 'Has OEC',
            dataIndex: 'OECFlag',
            xtype: 'checkcolumn',
            width: 50,
            filter: {
                type: 'boolean',
                active: true
            },
            flex: 1,
            sortable: true,
            hideable: false
        },
           {
               text: 'Action',
               dataIndex: 'ID',
               renderer: function (value) {
                   return '<a href="DepartmentEdit/' + value + '">Edit</a>';
               },
               hideable: false
           }],
        forceFit: false,
        split: true,
        renderTo: 'departmentSearchGrid',
        frame: false,
        width: 1300,
        plugins: ['gridfilters']
    });
    

    http://docs.sencha.com/extjs/5.0/core_concepts/classes.html