2017-09-18 55 views
0

我有一個從回看API獲取數據的應用程序。我在數據中獲得User ObjectID,但是我希望獲取用戶的DisplayName而不是ObjectID。拉力賽回顧API不允許水合用戶字段,所以在加載回顧API數據時我不能擁有用戶名。 我試圖用用戶對象ID過濾器創建商店來獲取用戶名,但我沒有得到用戶的名稱,直到回顧API數據加載完成。到回顧API數據加載完成時,我的數據操作也完成了。所以在第一次加載後加載用戶名是沒有用的。我嘗試將Store的異步屬性設置爲false/true,但沒有成功。 任何方式來完成這與拉力SDK 2.0? 這裏是代碼塊 -如何在CA Agile Central(AKA Rally)自定義應用程序中使用其ObjectID獲取單個對象詳細信息?

//Lookback call 
_getStoriesByUser: function (user_story_ids, user_oids, user_story_state, users) { 
    this.acceptedByTM = []; 
    var scheduleStates = []; 
    scheduleStates.push('Accepted'); 
    scheduleStates.push('Released to Prod'); 

    var deferred = Ext.create('Deft.Deferred'); 
    var me = this; 
    console.time("FetchingLBData"); 
    var snapshotStore = Ext.create('Rally.data.lookback.SnapshotStore', { 
     "context": this.getContext().getDataContext(), 
     "fetch": ["ScheduleState", "_User", "_PreviousValues.ScheduleState"], 
     "hydrate": ["ScheduleState"], 
     "find": { 
      "ObjectID": { "$in": user_story_ids }, 
      //"_User": { "$in": user_oids }, 
      "_TypeHierarchy": "HierarchicalRequirement", 
      "ScheduleState": { "$in": scheduleStates }, 
      "_PreviousValues.ScheduleState": { "$ne": null }, 
      "Children": null 
     }, 
     "sort": { "_ValidFrom": -1 } 
    }); 

    snapshotStore.load({ 
     callback: function (records, operation) { 
      if (operation.wasSuccessful()) { 
       console.timeEnd("FetchingLBData"); 
       me.logger.log("LB US Count >>", records.length, records) 
       var total_us_accepted_by_tm = 0; 
       var tmAcceptedUS = 0; 
       var nontmAcceptedUS = 0; 
       var tm_accepted = []; 
       Ext.Array.each(user_story_state, function (uss) { 
        Ext.Array.each(records, function (rec) { 
         if ((rec.get('ObjectID') == uss.ObjectID && rec.get('ScheduleState') == 'Accepted') && (uss.ScheduleState == 'Accepted' || uss.ScheduleState == 'Released to Prod')) { 
          total_us_accepted_by_tm += 1; 
          tm_accepted.push({ ObjectID: rec.get('ObjectID'), UserObjectID: rec.get('_User') }); 
           var userName; 
           //userName = me._fetchUserName(rec.get('_User')).value; 
           me._fetchUserName(rec.get('_User')).then({ 
            success: function (records) { 
             userName = records[0].get('DisplayName') 
            }, 
            failure: function (error) { 
             console.log('Error in Execution:', error) 
            } 
           }).always(function() { 
            console.log('Waiting for Update!'); 
           }); 
           //userName = rec.get('_User').DisplayName; 
           console.log('Accepted By Name: ', userName); 

           me.acceptedByTM.push({ ObjectID: rec.get('ObjectID'), UserObjectID: userName }); 
           return false; 

         } 
        }); 

       }); 

       deferred.resolve(me.acceptedByTM); 
      } else { 
       deferred.reject('Problem querying lookback'); 
      } 
     }, 
     scope: me 
    }); 
    return deferred; 
}, 

//Get User Details for a given user 
_fetchUserName: function (userObjId) { 
    var me = this; 
    var userName; 
    var userDetails; 
    var deferred = Ext.create('Deft.Deferred'); 
    me.logger.log("Fetching User Information for User >>", userObjId); 

    var userFilter = Ext.create('Rally.data.wsapi.Filter', { 
     property: 'ObjectID', 
     value: userObjId 
    }); 

    userDetails = Ext.create('Rally.data.wsapi.Store', { 
     model: 'User', 
     fetch: ['ObjectID', 'DisplayName'], 
     filters: userFilter, 
     limit: 1, 
     pageSize: 1 
    }); 

    userDetails.load({ 
     scope: this, 
     callback: function (records, operation, success) { 
      console.log("Operation Status: ", operation.synchronous); 
      if (success) { 
       //userName = records[0].get('DisplayName'); 
       deferred.resolve(records); 
      } else { 
       deferred.reject(Ext.String.format("Error getting {0} count for {1}: {2}", 'User', userFilter.toString(), operation.error.errors.join(','))); 
      } 
     } 
    }); 

    return deferred.promise; 
} 
+0

你能分享你到目前爲止的代碼?這樣做不應該太難。一旦你的回顧商店被加載,你只需要遍歷數據並從wsapi加載用戶。這聽起來像你已經有很多,也許只是需要一些幫助管理所有的異步代碼? –

+0

凱爾你好,我已經在原始文章中包含了代碼。 – Lokesh

+0

@KyleMorse,你有沒有找到任何解決方案?我嘗試通過在應用程序的加載中包含Deft.Promise.All將活動用戶和禁用用戶加載到本地變量中。但是,在完成回顧數據加載後,用戶會被加載。 – Lokesh

回答

0

爲了簡化事情,我很可能只是加載所有用戶前面的和WSAPI通過OBJECTID在地圖上標註他們。這樣您就可以獲得所有數據,並且可以在處理回溯快照時直接進行查找。

事情是這樣的:

Ext.define('CustomApp', { 
    extend: 'Rally.app.App', 
    componentCls: 'app', 
    launch: function() { 
     this._loadUsersFromWsapi().then({ 
      success: function (users) { 
       this.usersByOid = _.indexBy(users, function(user) { 
        return user.getId(); 
       }); 
       this._loadSnapshotData(); 
      }, 
      scope: this 
     }); 
    }, 

    _loadUsersFromWsapi: function() { 
     return Ext.create('Rally.data.wsapi.Store', { 
      model: 'user', 
      limit: Infinity, 
      pageSize: 2000, 
      fetch: ['ObjectID'] 
     }).load(); 
    }, 

    _loadSnapshotData: function() { 
     //Now in here for any given snapshot you can get the user 
     //information by looking it up in the this.usersByOid map 
     var userName = this._usersByOid[userObjectID].get('_refObjectName'); 
    } 
}); 
+0

非常感謝凱爾。它像一個魅力。但是,它會影響應用程序的性能,因爲我們正在獲取所有用戶數據,這正是我所需要的。有沒有其他方式可以用Lookback獲取用戶名?我知道我們無法保護用戶字段,但有一些地方我們可以通過其對象ID獲取所需的用戶名,以保持應用程序的最佳性能。 – Lokesh

+0

有沒有辦法通過回頭來獲取它。你可以做的是使用Deft.Promise.all並行回溯和用戶請求,然後在成功處理程序中將數據重新拼接在一起,而不是像上面的代碼那樣連續地創建請求。^^ –

相關問題