2

我正在尝试使用 setSrc 更改图像的来源,但出现此错误: Uncaught TypeError: Cannot call method 'setSrc' of undefined

Ext.Ajax.request.success Ext.apply.callback Ext.define.onComplete Ext.define.onStateChange(匿名函数)

这是我的源代码:查看页面:

Ext.define('MechanicalTerk.view.Picture', {
extend: 'Ext.Container', 
xtype: 'Picture',
requires: [
    'Ext.data.Store'
],
config: {
    layout: 'vbox', 
    items: 
    [
        {
            xtype: 'toolbar',
            docked: 'top',
            title: 'Requests',
            minHeight: '60px',
            items: [
                {
                    xtype:'button',
                    ui:'back button',
                    id:'backButton',
                    text:'Back'


                },{
                    xtype: 'button',
                    id:'logoutButton', 
                    text: 'Logout'
                }
            ],          
        },


        {
            xtype:'image',
            height:500,
            id:'layoutImage'
        }  
    ]
}
});

控制器:

Ext.define('MechanicalTerk.controller.PictureController',{
extend:'Ext.app.Controller',
showPicture: function(userID,sentAt){
    Ext.Ajax.request({
        url:'app/Model/database.php',
        params: {
            functionID: 3,
            userID: userID,
            sentAt: sentAt,
        },
        success: function (response, opts){
Ext.getCmp('layoutImage').setSrc('http://www.sencha.com/img/20110215-feat-perf.png');
Ext.Viewport.setActiveItem(Ext.create('MechanicalTerk.view.Picture'));
            }
        });
    }
});

有任何想法吗 ?

4

1 回答 1

0

您应该考虑不使用自己的id,而是使用itemId. 这可能是因为您试图重新使用已被破坏的组件。我建议ref在您的控制器中为您的图像创建一个,以便您可以访问它。

所以我在想:

Ext.define('MechanicalTerk.controller.PictureController',{
    extend:'Ext.app.Controller',

    config: {
        refs : {
            myImage : {
                autoCreate: true,
                selector: '#myimage',//assuming your image's itemId is 'myimage'
                xtype: 'image'
            }
        }
    },
    showPicture: function(userID,sentAt){
        Ext.Ajax.request({
            url:'app/Model/database.php',
            params: {
                functionID: 3,
                userID: userID,
                sentAt: sentAt,
            },
            success: function (response, opts){
                var me = this;
                me.getMyImage().setSrc('http://www.sencha.com/img/20110215-feat-perf.png');
                Ext.Viewport.setActiveItem(Ext.create('MechanicalTerk.view.Picture'));
            }
        });
    }
});

PS:未经测试,但尝试这种方法。

于 2013-04-07T18:53:51.573 回答