2015-02-10 26 views
0

我有一個Breeze和Nancy的Angular JS應用程序(Owin自託管)。 我已經想出瞭如何使用Breeze從服務器獲取數據,但是現在我試圖使用Breeze保存更改並且遇到問題。 我見過MVC的例子,如:如何使用Breeze js和Nancy實現SaveChanges

[HttpPost] 
public SaveResult SaveChanges(JObject saveBundle) 
{ 
    return _repository.SaveChanges(saveBundle); 
} 

但很明顯,我不能做同樣的南希。我的應用程序發送POST請求的SaveChanges但隨後TypeError: undefined is not a functionCannot read property 'map' of undefined

打破此刻,我簡單地返回相同的Json我的請求得到的,因爲我不知道的反應應該是什麼:

Post["/breeze/SaveChanges"] = parameters => 
{ 
    string response = "failed"; 
    try 
    { 
     response = new StreamReader(this.Request.Body).ReadToEnd(); 
    } 
    catch (Exception ex) 
    { 
     //TODO handle 
    } 
    return Response.AsJson(response); 
}; 

我不確定它是否因爲從服務器接收到不正確的請求或者因爲我沒有正確設置某些東西而中斷。

任何人都可以幫忙嗎?

回答

1

您可以只返回傳入saveBundle - 差不多。當Breeze客戶端收到來自服務器的保存響應時,它預計它有兩個屬性:entitieskeyMappings。實體已包含在saveBundle中,但您需要添加keyMappings數組(可以爲空)。

傳入saveBundle看起來是這樣的:

{ 
    "entities": [ 
    { 
     "OrderId": "4b143db9-6dd4-4c0e-90eb-97520d3694ac", 
     "CustomerId": "9ef1c520-318a-4b8a-b99d-cb9f6bdb22cc", 
     "OrderDate": "2015-01-30T08:00:00.000Z", 
     "entityAspect": { 
     "entityTypeName": "Order:#Northwind.Model", 
     "defaultResourceName": "Orders", 
     "entityState": "Added", 
     "originalValuesMap": { 
     }, 
     "autoGeneratedKey": null 
     } 
    }, 
    { 
    ...more entities... 
    } 
    ], 
    "saveOptions": { 
    "tag": "whatever" 
    } 
} 

傳出saveResult看起來是這樣的:

{ 
    "entities": [ 
    { 
     "OrderId": "4b143db9-6dd4-4c0e-90eb-97520d3694ac", 
     "CustomerId": "9ef1c520-318a-4b8a-b99d-cb9f6bdb22cc", 
     "OrderDate": "2015-01-30T08:00:00.000Z", 
    }, 
    { 
    ...more entities... 
    } 
    ], 
    "keyMappings": [ 
    ] 
} 

注意傳入saveBundle對描述實體每個實體的entityAspect。 saveResult不需要這樣做,但它不會有害,並且在客戶端上將被忽略,saveOptions也是如此。

這些格式記錄在Breeze文檔的DataServiceAdapters部分,但可以理解的是您沒有找到它們。

+0

我試圖返回這個響應,但它並沒有幫助,在接收到任何響應之前和之後的另一個響應之前,錯誤仍然被拋出。我在這裏創建了另一個問題:http://stackoverflow.com/questions/28500013/angularjs-throws-exception-when-trying-to-savechanges-with-breeze我非常感謝,如果你會看看那。謝謝。 – Burjua 2015-02-13 12:52:36