显示上一个记录值,并非所有值都循环

时间:2014-09-22 06:12:53

标签: javascript extjs extjs4 rally

在下面的代码中,_copyChild和innerModelRetrieved函数在控制台4上逐个打印功能,但在下一个函数onInnerModelRetrieved打印4次最后一个特征值时,我无法弄清楚为什么会发生这种情况。请帮帮我。

        Ext.define('CustomApp', {               
            extend: 'Rally.app.App',
            componentCls: 'app',
            _newObj : {},
            childrens: [],
            _type : null,
            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','PortfolioItem/Epic', 'PortfolioItem/Program'],
                    autoShow: true,
                    storeConfig:{
                        fetch: ['Name','PortfolioItemTypeName']
                    },
                    listeners: {
                        artifactChosen: function(selectedRecord) {
                            childrens = [];
                            this._type = selectedRecord.get('PortfolioItemTypeName');
                            this._newObj = selectedRecord;
                            this.onqModelRetrieved();
                            var self = this;
                            Ext.create('Rally.data.wsapi.Store', {
                                model: 'PortfolioItem/' + selectedRecord.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;
                                            childrens = item.getCollection('Children');
                                            childrens.load({
                                                fetch: ['FormattedID'],
                                                callback: function(records, operation, success){
                                                    Ext.Array.each(records, function(portfolioitem){
                                                        if (portfolioitem.get('PortfolioItemTypeName') == "Feature") {
                                                            self._childObj = portfolioitem;
                                                            self._copyChild();
                                                        }   
                                                    }, self);   
                                                },
                                                scope: this 
                                            });     
                                        }, self);
                                    }   
                                }
                            });
                        },
                        scope: this
                    },
                }); 
            },
            // Inner Copy functions
            _copyChild: function() {
                console.log("child value here", that._childObj);
                this.innerModelRetrieved();
            },
            innerModelRetrieved: function() {
                var that = this
                console.log("next child value here", that._childObj);
                that._type = 'PortfolioItem/' + that._childObj.get('PortfolioItemTypeName');
                Rally.data.ModelFactory.getModel({
                    type: that._type,
                    success: that.onInnerModelRetrieved,
                    scope: that
                });     
            },                      
            onInnerModelRetrieved: function(model) {
                console.log("next child value here", this._childObj);
                this.model = model;
                this.genericInnerCopy(model);
            },

1 个答案:

答案 0 :(得分:1)

为了完成这项工作,您需要创建一个块作用域和一个设置为当前childObj的局部变量,否则onInnerModelRetrieved只获取childObj的最后一个值,因为它等待迭代结果在它开始之前完成。

功能

(function(){...})();

立即调用创建该块范围和

var child = that._childObj

在每次迭代时捕获单个对象。

最后,孩子通过

传递

success: function(model)

使用onInnerModelRetrievedmodel两个参数调用child

innerModelRetrieved: function() {
    var that = this;
    (function(){
         var child = that._childObj;
         console.log("in innerModelRetrieved, that._childObj.data.FormattedID:", that._childObj.data.FormattedID);
          that._type = 'PortfolioItem/' + that._childObj.get('PortfolioItemTypeName');
          Rally.data.ModelFactory.getModel({
          type: that._type,
          success: function(model){
               that.onInnerModelRetrieved(model, child );
          },
          scope: that
          });
       })();
},                      
onInnerModelRetrieved: function(model, _childObj ) {
       console.log("in onInnerModelRetrieved, that._childObj.data.FormattedID:", _childObj.data.FormattedID);
       this.model = model;                
}

以下是更改前的屏幕截图:

enter image description here

以下是更改后的屏幕截图:

enter image description here