显示最后记录值,并非所有值都循环 [英] Last record value is displayed, not all values are looped

查看:105
本文介绍了显示最后记录值,并非所有值都循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,在_copyChild和innerModelRetrieved函数中,一个接一个地在控制台4上打印功能,但是在下一个函数onInnModelRetrieved中打印了最后一个特征值的4倍,我无法弄清它为什么这样发生。请帮助我。

  Ext.define('CustomApp',{
extends:'Rally.app。应用程序'
componentCls:'app',
_newObj:{},
childrens:[],
_type:null,
启动:function(){
Ext.create('Rally.ui.dialog.ChooserDialog',{
width:450,
autoScroll:true,
height:525,
title:'Select复制',
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);
},
范围:this
});
},self);
}
}
});
},
范围:此
},
});
},
//内部复制函数
_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
});
},
onInModelRetrieved:function(model){
console.log(next child value here,this._childObj);
this.model = model;
this.genericInnerCopy(model);
},


解决方案

您需要创建一个块范围和一个设置为当前 childObj 的局部变量的工作,否则 onInModelRetrieved 仅获取childObj的最后一个值,因为它等待迭代结果在踢进之前完成。



功能



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



立即调用创建块范围和



var child = that._childObj



每个迭代中的单个对象。



最后,孩子通过



success:function(model)



它调用 onInModelRetrieved 与两个参数, model 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
});
})() ;
},
onInModelRetrieved:function(model,_childObj){
console.log(inInnerModelRetrieved,that._childObj.data.FormattedID:,_childObj.data.FormattedID);
this.model = model;
}

这是更改前的屏幕截图:





这里是更改后的屏幕截图:




In the code below, in _copyChild and innerModelRetrieved functions print on console 4 features one by one, but in next function onInnerModelRetrieved 4 times last feature value is printed, I am not able to figure it why its happening like that. please help me with this.

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

解决方案

In order to make this work you need to create a block scope and a local variable that is set to the current childObj, otherwise onInnerModelRetrieved gets only the last value of childObj since it waits for the iterations over results to complete before it kicks in.

The function

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

immediately invoked creates that block scope and

var child = that._childObj

captures individual objects at each iteration.

Finally, the child is passed via

success: function(model)

which invokes onInnerModelRetrieved with two parameters, model and 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;                
}

Here is the screenshot before the changes:

And here is the screenshot after changes:

这篇关于显示最后记录值,并非所有值都循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆