错误:无法调用未定义的方法'createChild'

时间:2012-12-06 11:12:56

标签: javascript browser extjs

我编写的ExtJS4类定义如下:

    Ext.define('Ext.ux.PictureBox', {
        extend: 'Ext.panel.Panel',
        alias: 'widget.picturebox',    
        url: "",   

        afterRender: function () {
            this.callParent();
            this.imageEl = Ext.DomHelper.append(this.body, Ext.String.format("<img style='width:{0}px;height:{1}px' src='{2}' />", this.width, this.height, this.url), true);

       this.items.items[0].addCls('browse-picture-button');
       this.items.items[1].addCls('remove-picture-button');    

        },

     initComponent: function() {

     var conf = {
        items:[
        {
          itemid: 'browseBtn',
          xtype: 'fileuploadfield',
          name: 'photo',
            buttonText: '...',
            buttonOnly: true,
          text: '...',    
            width: 30,
            height: 20
        },

         {
         itemid: 'removeBtn',
         xtype: 'button',
         text: 'X',
             width: 30,
             height: 20
         }        
        ]       
    };        

     Ext.apply(this, Ext.apply(this.initialConfig, conf));
     this.callParent(arguments);     
        },

 setSize: function (w, h) {
        this.callParent();
        if (this.imageEl) {
            this.imageEl.setSize(w, h);
        }
    },
 setUrl: function (url) {
        this.url = url;
        if (this.rendered) {
            this.imageEl.dom.src = url;
        }
    }
});

然后,我创建了这个类:

Ext.create('Ext.ux.PictureBox', {   
    width: 300,
    height: 300,       
    url: 'http://blogs-images.forbes.com/daviddisalvo/files/2012/01/googlelogo2.jpg',
    renderTo: Ext.getBody()
});​

脚本抛出错误无法调用未定义的方法'createChild'。为什么?

如果我使用'button而不是fileuploadfield;

,一切正常

Yuo可以在此处查看并运行此示例http://jsfiddle.net/C3HEu/12/

1 个答案:

答案 0 :(得分:0)

您需要在Ext.onReady()内运行所有Ext代码。 http://jsfiddle.net/C3HEu/22/

或者,让Ext在浏览器的onDomReady事件中运行,而不是onLoadhttp://jsfiddle.net/C3HEu/24/