How to implement SaveChanges with Breeze js and Nancy

蓝咒 提交于 2019-12-12 03:25:30

问题


I have an Angular JS application with Breeze and Nancy (self-hosted with Owin). I've figured out how to get data from server with Breeze, but now I'm trying to save changes using Breeze and have problems with it. I've seen MVC examples like:

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

But obviously I can't do the same with Nancy. My application sends POST request to SaveChanges but then breaks with TypeError: undefined is not a function and Cannot read property 'map' of undefined

At the moment I simply return the same Json I get in Request, as I have no idea what the response should be:

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

I'm not sure if it breaks because it receives incorrect request from server or because I haven't set something up correctly.

Can anyone help with it?


回答1:


You can just return the incoming saveBundle -- almost. When the Breeze client receives the save response from the server, it expects it to have two properties: entities and keyMappings. The entities are included in the saveBundle already, but you'll need to add the keyMappings array (which can be empty).

The incoming saveBundle looks like this:

{
  "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"
  }
}

The outgoing saveResult looks like this:

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

Note that the incoming saveBundle has an entityAspect on each entity that describes the entity. The saveResult does not need this, but it's not harmful and will be ignored on the client, as will the saveOptions.

These formats are documented in the DataServiceAdapters section of the Breeze documentation, but it's understandable that you didn't find them.



来源:https://stackoverflow.com/questions/28440285/how-to-implement-savechanges-with-breeze-js-and-nancy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!