【问题标题】:Last record value is displayed, not all values are looped显示最后一条记录值,并非所有值都循环
【发布时间】:2014-09-22 06:12:52
【问题描述】:

在下面的代码中,在控制台 4 上的 _copyChild 和 innerModelRetrieved 函数中逐一打印功能,但在下一个功能 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);
            },

【问题讨论】:

  • 你应该清理你的代码并发布一个可以正确格式化的例子。就目前而言,它是一团乱麻。
  • @existdissolve - 对不起..我会编辑并再次发布。
  • @existdissolve - 我认为现在代码更具可读性。你能理解它吗?

标签: javascript extjs extjs4 rally


【解决方案1】:

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

功能

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

立即调用创建该块范围和

var child = that._childObj

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

最后通过子传递

success: function(model)

它使用两个参数调用onInnerModelRetrievedmodelchild

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;                
}

这是更改前的屏幕截图:

这是修改后的截图:

【讨论】:

  • 你真是太棒了。我也尝试过成功作为功能,但它仍然无法正常工作。万分感谢。我在等这个。你让我开心。
猜你喜欢
  • 2017-07-15
  • 2015-11-26
  • 2019-02-20
  • 2012-09-14
  • 1970-01-01
  • 1970-01-01
  • 2021-03-29
  • 1970-01-01
相关资源
最近更新 更多