2013-04-22 79 views
3

我一直在構建我的第一個SPA應用程序,從John Papa的快速啓動中跳出了偉大的工作。可能不會設置收藏導航屬性

我遇到的問題是,當我嘗試通過微風執行展開操作時,或者只是選擇所有的字段時,在我的子集合映射過程中,我遇到了來自Breeze的問題。 newValue.entityAspect是undefined

如果我通過select子句通過我的api查詢,一切都很好,如果我直接調用表'位置',我會在chartVal對象上得到導航錯誤。 enter image description here enter image description here

var query = EntityQuery.from('Positions') 
      //.select('id, endDate, gauge, hoursPerWeek, memberId, title, summary, startDate, totalHours, weightedHours, company, compensation') 
      .where('memberId', '==', id) 
      .expand('Company, ChartVals') 
      .orderBy(orderBy.positions); 

      return manager.executeQuery(query) 
       .then(querySucceeded) 
       .fail(queryFailed); 

      function querySucceeded(data) { 
       var list = partialMapper.mapDtosToEntities(manager, data.results, entityNames.position, 'id'); 
       if (observables) { 
        observables(list); 
       } 

上面的查詢會成功,然後對MapDtosToEntities通話(約翰·帕帕提供)

功能mapDtosToEntities(經理,DTOS,的entityName,註冊表,extendWith){ 回報DTOS .MAP(dtoToEntityMapper);

 function dtoToEntityMapper(dto) { 
      var keyValue = dto[keyName]; 
      var entity = manager.getEntityByKey(entityName, keyValue); 
      if (!entity) { 
       // We don't have it, so create it as a partial 
       extendWith = $.extend({ }, extendWith || defaultExtension); 
       extendWith[keyName] = keyValue; 
       entity = manager.createEntity(entityName, extendWith); 
      } 
      mapToEntity(entity, dto); 
      entity.entityAspect.setUnchanged(); 
      return entity; 
     } 

     function mapToEntity(entity, dto) { 
      // entity is an object with observables 
      // dto is from json 
      for (var prop in dto) { 
       if (dto.hasOwnProperty(prop) && prop !="entityAspect") { 
        entity[prop](dto[prop]); <-- tanks here on children 
       } 
      } 
      return entity; 
     } 
    } 


/// <summary> 
/// TODO: Update summary. 
/// </summary> 
public class Position : PersistableObject 
{ 
    public int CompanyId { get; set; } 

    /// <summary> 
    /// Weighted hours based on Half-Life. 
    /// </summary> 
    public int CreditMinutes { get; set; } 

    public int? CompensationId { get; set; } 

    public DateTime? EndDate { get; set; } 
    /// <summary> 
    /// Code to hold measure of completeness. 10 = complete 0 = not complete. 
    /// </summary> 
    public int Gauge { get; set; } 

    public int? HoursPerWeek { get; set; } 

    public bool IsCurrent { get; set; } 

    [StringLength(40)] 
    public string LinkedInId { get; set; } 

    [Required] 
    public int MemberId { get; set; } 

    [StringLength(40)] 
    public string Title { get; set; } 

    [StringLength(400)] 
    public string WeightedWords { get; set; } 

    /// <summary> 
    /// Adjusted Experience score Based on half life 
    /// </summary> 
    public int Score { get; set; } 

    [Required] 
    [StringLength(2000)] 
    public string Summary { get; set; } 

    public DateTime StartDate { get; set; } 

    public decimal? SalaryEnd { get; set; } 

    /// <summary> 
    /// Hourly salary - let users enter hourly, monthly, annual etc 
    /// </summary> 
    public decimal? SalaryStart { get; set; } 

    public decimal TotalHours { get; set; } 

    /// <summary> 
    /// Total Man-hours Multiplied by Gauge reading 
    /// </summary> 
    public decimal WeightedHours { get; set; } 

    public int VisibilityId { get; set; } 

    public Company Company { get; set; } 

    public Member Member { get; set; } 

    public virtual Compensation Compensation { get; set; } 

    public virtual ICollection<Project> Projects { get; set; } 

    public virtual ICollection<Tag> Tags { get; set; } 

    public virtual ICollection<ChartVal> ChartVals { get; set; } 

} 


public class ChartVal : PersistableObject 
{ 
    [Required] 
    [StringLength(40)] 
    public string Key { get; set; } 

    public double Value { get; set; } 

    public string Percentage { get; set; } 

    public int PositionId { get; set; } 

    public virtual Position Position { get; set; } 
} 

感謝您花點時間檢查我的問題。我感謝你的時間!

回答

1

我也看到了這一點。我相信解決方案將涉及檢測屬性是否爲集合並迭代它以逐個添加項目。我還沒有嘗試這樣做,因爲我還是老樣子試圖決定,如果它是正確事情

我不打算將此作爲每本身,而是作爲一個指示的答案,別人也跟着同跟蹤作爲海報,並會對社區的任何迴應感興趣。

0

到底,我結束了放棄的約翰·帕帕mapDtosToEntities映射器的方法,只是遍歷我的收藏...

function querySucceeded(data) { 
       var list = []; 
       for(i = 0; i< data.results.length; i++) 
       { 
        list.push(data.results[i]); 
       } 
       observables(list); 
      } 
1

我有一個類似的問題,有一個「imageUser」的部分用戶實體導航屬性。

我剛剛改變了John Papa的mapDtosToEntities「mapToEntity」函數,並且它似乎在工作 - 我對這一切都很陌生,所以我不能保證導航屬性與保存更改有關的完整性,但它是絕對的映射關聯,因爲我創建了一個'imageUrl'計算屬性,用於檢索第一個關聯並將其顯示在圖像元素中(即看到的是相信):

 function mapToEntity(entity, dto) { 
      // entity is an object with observables 
      // dto is from json 
      for (var prop in dto) { 
       if (dto.hasOwnProperty(prop)) { 
        if (typeof(dto[prop]) != "object") { 
         entity[prop](dto[prop]); 
        } else { 
         entity[prop] = ko.observable(dto[prop]); 
        } 
       } 
      } 

      return entity; 
     }