2014-09-22 65 views
2

在下面的代码中,在_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); 
      }, 
+0

应该清理一下你的代码,并张贴,可以正确格式化的例子。目前它存在,这是一个乱七八糟的混乱。 – existdissolve 2014-09-22 11:50:16

+0

@existdissolve - 对不起。我将编辑并重新发布。 – Sontya 2014-09-22 11:54:04

+0

@existdissolve - 我认为现在代码更具可读性。你能从中理解吗? – Sontya 2014-09-22 13:00:08

回答

1

为了使这项工作,你需要创建一个块范围,并且被设置为当前childObj一个局部变量,否则onInnerModelRetrieved只获得childObj的最后一个值,因为它在结果等待迭代完成它在踢之前。

功能

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

立即调用创建块范围和

var child = that._childObj

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

enter image description here

最后,孩子通过

success: function(model)

它调用onInnerModelRetrieved有两个参数,modelchild

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

+0

你真棒。我也试过成功的功能,但仍然无法正常工作。万分感谢。我正在等待这个。你让我今天一整天都感觉很好。 – Sontya 2014-09-25 04:49:41

+0

不客气 – nickm 2014-09-25 14:26:43

相关问题