2017-02-16 58 views
1

所以我有兩個視圖之間的主/細節方案。母版頁顯示一個列表,在點擊其中一個項目後,我通過Aurelia的EventAggregator發送一條消息到子視圖,並帶有一個反序列化的dto(來自主設備的選定項目)作爲消息的有效載荷。Javascript ServiceStack客戶機序列化錯誤

但是,當我然後嘗試將該項目作爲子項中後續請求的參數(以獲取其他信息)時,有效內容對象無法序列化。

Master.ts:

import { JsonServiceClient } from "servicestack-client"; 
import { 
    ListPendingHoldingsFiles, 
    ListPendingHoldingsFilesResponse, 
    SendHoldings, 
    PositionFileInfo 
} from "../holdingsManager.dtos"; 
import { inject, singleton } from "aurelia-framework"; 
import { Router } from "aurelia-router"; 
import { EventAggregator } from "aurelia-event-aggregator"; 
import { GetPendingPositionMessage } from "../common/GetPendingPositionMessage"; 

@singleton() 
@inject(Router, EventAggregator) 
export class Pending { 
    router: Router; 
    positions: PositionFileInfo[]; 
    client: JsonServiceClient; 
    eventAgg: EventAggregator; 

    constructor(router, eventAggregator) { 
     this.router = router; 
     this.eventAgg = eventAggregator; 
     this.client = new JsonServiceClient('/'); 
     var req = new ListPendingHoldingsFiles(); 
     this.client.get(req).then((getHoldingsResponse) => { 
      this.positions = getHoldingsResponse.PositionFiles; 
     }).catch(e => { 
      console.log(e); // "oh, no!" 
     }); 
    } 

    openHoldings(positionInfo) { 
     this.eventAgg.publish(new GetPendingPositionMessage(positionInfo)); 
     this.router.navigate('#/holdings'); 
    } 
} 

Child.ts:

import { JsonServiceClient } from "servicestack-client"; 
import { inject, singleton } from "aurelia-framework"; 
import { Router } from 'aurelia-router'; 
import { EventAggregator } from "aurelia-event-aggregator"; 
import { GetPendingPositionMessage } from "../common/GetPendingPositionMessage"; 
import { 
    GetPendingHoldingsFile, 
    GetPendingHoldingsFileResponse, 
    Position, 
    PositionFileInfo 
} from "../holdingsManager.dtos"; 

@singleton() 
@inject(Router, EventAggregator) 
export class Holdings { 
    router: Router; 
    pendingPositionFileInfo: PositionFileInfo; 
    position: Position; 
    client: JsonServiceClient; 
    eventAgg: EventAggregator; 

    constructor(router, eventAggregator) { 
     this.router = router; 
     this.eventAgg = eventAggregator; 
     this.eventAgg.subscribe(GetPendingPositionMessage, 
      message => { 
       this.pendingPositionFileInfo = message.fileInfo; 
      }); 
    } 

    activate(params, routeData) { 
     this.client = new JsonServiceClient('/'); 
     var req = new GetPendingHoldingsFile(); 
     req.PositionToRetrieve = this.pendingPositionFileInfo; 
     this.client.get(req).then((getHoldingsResponse) => { 
      this.position = getHoldingsResponse.PendingPosition; 
     }).catch(e => { 
      console.log(e); // "oh, no!" 
     }); 
    } 
} 

所以,當孩子激活並試圖發送請求 'GetPendingHoldingsFile' 錯誤發生。

Failed to load resource: the server responded with a status of 500 (NullReferenceException) 

我已驗證this.pendingPositionFileInfo在孩子不爲空或空,並且在服務器端,沒有接收對象(它爲空)。我是Aurelia的新手,並且對Javascript使用經驗不足,所以我必須缺少一些東西,任何建議都不勝感激。

編輯1

這似乎是壞了我如何與ServiceStack交互。我正在使用serviceStack版本4.5.6的[email protected]^0.07。我嘗試新建一個dto(PositionFileInfo)的新副本,並複製父視圖中的所有值,以確保沒有發生某些我不知道的JavaScript類型轉換奇怪事件,但即使使用了新鮮dto webservice仍然收到一個空請求。

+0

不要在這裏使用Event Aggregator ...太複雜和冗餘的路由器。只需保存當前記錄'this.positionInfo = positionInfo;',然後路由到您的孩子並讓您的孩子導入/注入父母並獲取當前記錄。 – LStarky

+0

幾個很好的演示類似的情況在這裏:http://stackoverflow.com/questions/42260833/aurelia-how-can-i-modify-sidebar-content-from-inside-a-router-view – LStarky

+0

感謝您的提示,不幸的是,這並不能解決序列化問題。 – Bitfiddler

回答

1

從'client.get(...)'切換到'client.post(...)'修復了這個問題。顯然試圖在URL中序列化對象並不是一個好計劃。

+0

絕對是一個更好的計劃!我沒有注意到你的代碼,但你完全正確。 – LStarky