调用函数时出现错误“Uncaught TypeError:undefined不是函数”

时间:2014-09-18 10:19:39

标签: javascript extjs rally

以下是我的代码,我在调用函数时遇到了一些错误,但我无法找到它,请帮助我。

this._copyChild(final_features);

上面的代码行在下面的程序中给出了上述错误,请参阅代码中的箭头

            launch: function() {
                Ext.create('Rally.ui.dialog.ChooserDialog', {
                    width: 450,
                    autoScroll: true,
                    height: 525,
                    title: 'Select to Copy',
                    pageSize: 100,
                    closable: false,
                    selectionButtonText: 'Copy',                  
                    artifactTypes: ['PortfolioItem/Feature','PortfolioItem/MMF'],
                    autoShow: true,
                    storeConfig:{
                        fetch: ['Name','PortfolioItemTypeName']
                    },
                    listeners: {
                        artifactChosen: function(selectedRecord) {
                            childrens = [];
                            this._type = selectedRecord.get('PortfolioItemTypeName');
                            this._newObj = selectedRecord;
                            this.onqModelRetrieved();
                            this.getChildrens(selectedRecord);
                        },
                        scope: this
                    },
                }); 
            },
            getChildrens: function(selectedRecord) {
                Ext.create('Rally.data.wsapi.Store', {
                    model: 'PortfolioItem/' + this._newObj.get('PortfolioItemTypeName'),
                    fetch: ['Name', 'FormattedID', 'Children'],
                    pageSize: 1,
                    autoLoad: true,
                    listeners: {
                        load: function(store, records) {
                            final_features = [];
                            Ext.Array.each(records, function(child){
                                var item = selectedRecord;
                                var childrens = item.getCollection('Children');
                                childrens.load({
                                    fetch: ['FormattedID'],
                                    callback: function(childrens, operation, success){
                                        Ext.Array.each(childrens, function(child){
                                            if (child.get('PortfolioItemTypeName') == "Feature") {
                                                final_features.push(child);
            =============>                      this._copyChild(final_features);
                                            }   
                                        }, this);   
                                    },
                                    scope: this     
                                });     
                            }, this);
                        }   
                    }
                });             
            },
            // Inner Copy functions
            _copyChild: function(final_features) {
                var that = child;
                this.innerModelRetrieved(child);
            },

1 个答案:

答案 0 :(得分:4)

each函数中,this不指向外部对象,而是指向listeners对象。

getChildrens函数的开头创建一个指向外部对象的局部变量,并用它替换所有范围参数。

getChildrens: function(selectedRecord) {
    var self = this; // <-- local copy of `this` (owner of getChildrens)
    Ext.create('Rally.data.wsapi.Store', {
    ...
            Ext.Array.each(childrens, function(child){
                if (child.get('PortfolioItemTypeName') == "Feature") {
                    final_features.push(child);
                    this._copyChild(final_features);
                }   
            }, self); // <--- replace `this` with `self`