如何在新创建的类Ext.define中使用嵌套方法

时间:2016-12-17 12:40:12

标签: extjs

你能教我如何调用嵌套方法吗?基本的想法,我希望在一个类中有一个函数,我可以通过发送另一个具有不同返回路径的folderSelector从外部配置。但是现在我正试图做那个extjs对我说:

[W] XTemplate evaluation exception: foldersSelector is not a function

代码示例:

Ext.define('somepath.Some1', {
extend: 'somepath.SomeParent',
text: 'sometext',
foldersSelector: function(data){
	return data.folders;
},
initComponent:  function(){
   ...
   this.callParent();
}
renderer: function(data){
	bla bla bla / got data from somewhere
	...
	...
	foldersSelector(data);
    // with this.foldersSelector result the same!
}
});

1 个答案:

答案 0 :(得分:1)

您的功能中缺少this。您需要从类对象中调用该函数。像这样:

Ext.define('MyApp.view.MyPanel', {
    extend: 'Ext.panel.Panel',
    alias: 'widget.mypanel',


    height: 250,
    width: 400,
    title: 'My Panel',
    defaultListenerScope: true,


    listeners: {
        render: 'onPanelRender'
    },

    onPanelRender: function(component, eOpts) {
        // call using this
        this.folderSelector();
        // call using component
        component.folderSelector();
    },

    folderSelector: function(data) {
        console.log('Function')
    }

});

小提琴https://fiddle.sencha.com/#view/editor&fiddle/1mpe

在您的情况下,您位于网格列中,您无法调用this,因为这是网格。你必须得到专栏。

Ext.define('MyApp.view.MyColumn3', {
    extend: 'MyApp.view.SuperColumn',
    alias: 'widget.mycolumn3',

    id: 'myColumnId',

    foldersSelector: function (data) {
        console.log('I AM CALLED');
        return data + '-SOSO'
    },

    renderer: function(value, metaData, record, rowIndex, colIndex, store, view) {
            // you have to get the column, there are many ways how to do so                
            var c = Ext.first('#myColumnId')
            return c.foldersSelector(value)
    }

    // option without the id
    renderer: function(value, metaData, record, rowIndex, colIndex, store, view) {     
            // this is the grid
            var gridColumns = this.getColumns();
            // we know the column index
            var c = gridColumns[colIndex];
            return c.foldersSelector(value)
    }


});

小提琴:https://fiddle.sencha.com/#view/editor&fiddle/1mpo