0
votes

I've been trying for a few days now to get the Breeze 1.4.9 to work with a rails back end in a different manner than the Breeze Ruby SPA sample. I would rather send bulk save changes instead of trying to send RESTful calls to the server on every entity change. To that end, I've written a rails controller/model methods that will parse out all the different entities in a Breeze SaveChanges POST and act accordingly. Everything works great except that the response to SaveChanges POST doesn't seem to satisfy all the checks for Breeze and EntityManager.hasChanges() is still true even after the response is processed successfully.

Here's a typical cycle: Breeze requests my hand crafted metadata and parses it fine:

{
  "metadataVersion": "1.0.5",
  "namingConvention": "rubyNamingConvention",
  "localQueryComparisonOptions": "caseInsensitiveSQL",
  "dataServices": [
    {
      "serviceName": "breeze\/Breeze\/",
      "hasServerMetadata": true,
      "jsonResultsAdapter": "webApi_default",
      "useJsonp": false
    }
  ],
  "structuralTypes": [
    {
      "shortName": "VarianceReason",
      "namespace": "Icon",
      "autoGeneratedKeyType": "Identity",
      "defaultResourceName": "VarianceReasons",
      "dataProperties": [
        {
          "name": "id",
          "dataType": "Int32",
          "isNullable": false,
          "defaultValue": 0,
          "isPartOfKey": true,
          "validators": [
            {
              "name": "required"
            },
            {
              "name": "int32"
            }
          ]
        },
        {
          "name": "name",
          "dataType": "String",
          "isNullable": false,
          "defaultValue": "",
          "maxLength": 256,
          "validators": [
            {
              "name": "required"
            },
            {
              "maxLength": 256,
              "name": "maxLength"
            }
          ]
        },
        {
          "name": "createdAt",
          "dataType": "DateTime",
          "isNullable": false,
          "defaultValue": "1900-01-01T08:00:00.000Z",
          "validators": [
            {
              "name": "required"
            },
            {
              "name": "date"
            }
          ]
        },
        {
          "name": "updatedAt",
          "dataType": "DateTime",
          "isNullable": false,
          "defaultValue": "1900-01-01T08:00:00.000Z",
          "validators": [
            {
              "name": "required"
            },
            {
              "name": "date"
            }
          ]
        }
      ]
    }
  ],
  "resourceEntityTypeMap": {
    "VarianceReasons": "VarianceReason:#Icon"
  }
}

I make an entity change in Breeze and it POSTs the below to rails when I call em.SaveChanges():

{
   "entities":[
      {
         "id":-1,
         "name":"anyuthingasd",
         "created_at":"1900-01-01T08:00:00.000Z",
         "updated_at":"1900-01-01T08:00:00.000Z",
         "entityAspect":{
            "entityTypeName":"VarianceReason:#Icon",
            "defaultResourceName":"VarianceReasons",
            "entityState":"Added",
            "originalValuesMap":{

            },
            "autoGeneratedKey":{
               "propertyName":"id",
               "autoGeneratedKeyType":"Identity"
            }
         }
      }
   ],
   "saveOptions":{

   }
}

Rails then responds with:

{
   "KeyMappings":[
      {
         "EntityTypeName":"VarianceReason:#Icon",
         "TempValue":-1,
         "RealValue":16
      }
   ],
   "Entities":[
      {
         "id":16,
         "name":"anyuthingasd",
         "created_at":"2014-05-02T14:21:24.221Z",
         "updated_at":"2014-05-02T14:21:24.221Z",
         "Entity":null
      }
   ]
}

Breeze then merges in the new id key mapping but doesn't clear the cache, so next time I make another entity change it still has the first change which has already persisted to the server and the new change. Can anyone tell me what I'm not responding with from the rails side that makes Breeze EntityManager not satisfied? I'm trying to trace through the 15k lines of code but can't say I'm a JS ninja.

1
Might be faster to load up one the sample applications that is using saveChanges and check out the response to see if there is anything being set. - PW Kad
not sure I understand completely @PWKad. I have got the Breeze ruby SPA app running, but this app is slightly different in that I'm not saving on every change. I'm basically just providing a server to client name mapping, but expect everything else to work as is. - Gerardo
I was mentioning it might have been easier to grab and run one of the samples that implements saveChanges with Web API to see what the return JSON looks like but it looks like Ward already copied the return for you below. - PW Kad

1 Answers

2
votes

We really do need to show folks how to build a data service adapter for whatever service they've got.

In this case, it appears you chose to implement something like the SaveChanges method in C# on the Web API. In other words, you've chosen to emulate the out-of-the-box Breeze protocol. That's cool! And non-trivial too so kudos to you.

I think what's missing from the entity JSON in your save response is the EntityType name. Breeze can't find the corresponding cached entities without knowing their types and thus cannot update their change-states.

Again, because you've decided to use the default Web API data service adapter, you'll want to return a response that adapter expects. That adapter defines a "jsonResultsAdapter" that expects each JSON entity data object to have a $type property specifying the full type name (namespace.typename).

In your example, I think you'd want to return

...
"Entities":[
  {         
     "$type": "Icon.VarianceReason",
     "id":16,
     "name":"anyuthingasd",
     "created_at":"2014-05-02T14:21:24.221Z",
     "updated_at":"2014-05-02T14:21:24.221Z",
  }
]

How about an example?

I suspect that you may not have easy access to a server with Web API that can show you what a save response looks like with the default adapter. Therefore, I've pasted below a Todo app's saveChanges request and response for a change-set that includes a new, a modified, and a deleted TodoItem.

The Request

Below is the payload of the POST request to the "SaveChanges" endpoint. It is probably way more verbose than you need (more verbose than I'd need). Just to pick one example, the "autoGeneratedKey" is of no interest to the server whatsoever.

I'm just showing you what the default data service adapter sends. Someday you'll write your own to do it the way you want it. For now I suppose there is no harm in sending too much crappola ... as long as you're happy to ignore it on the Rails end :-)

{
  "entities": [
    {
      "Id": 5,
      "Description": "Cheese",
      "CreatedAt": "2012-08-22T09:05:00.000Z",
      "IsDone": true,
      "IsArchived": false,
      "entityAspect": {
        "entityTypeName": "TodoItem:#Todo.Models",
        "defaultResourceName": "Todos",
        "entityState": "Deleted",
        "originalValuesMap": {

        },
        "autoGeneratedKey": {
          "propertyName": "Id",
          "autoGeneratedKeyType": "Identity"
        }
      }
    },
    {
      "Id": 6,
      "Description": "Modified Todo",
      "CreatedAt": "2012-08-22T09:06:00.000Z",
      "IsDone": false,
      "IsArchived": false,
      "entityAspect": {
        "entityTypeName": "TodoItem:#Todo.Models",
        "defaultResourceName": "Todos",
        "entityState": "Modified",
        "originalValuesMap": {
          "Description": "Wine"
        },
        "autoGeneratedKey": {
          "propertyName": "Id",
          "autoGeneratedKeyType": "Identity"
        }
      }
    },
    {
      "Id": -1,
      "Description": "New Todo",
      "CreatedAt": "2014-05-02T17:34:00.904Z",
      "IsDone": false,
      "IsArchived": false,
      "entityAspect": {
        "entityTypeName": "TodoItem:#Todo.Models",
        "defaultResourceName": "Todos",
        "entityState": "Added",
        "originalValuesMap": {

        },
        "autoGeneratedKey": {
          "propertyName": "Id",
          "autoGeneratedKeyType": "Identity"
        }
      }
    }
  ],
  "saveOptions": {

  }
}

The Response

The $id property is a node counter. It's useful when you have repeated entities so you don't have to worry about cycles or repeated entity data in your payload (an object with a $ref property is the placeholder for the repeated entity). You can ignore $id if you don't need this feature (and you rarely would need it in a save result).

Notice that the $type is in the .NET "CSDL" type format "namespace.typename", not the Breeze type format "typename:#namespace". This is an artifact of the data service adapter's jsonResultsAdapter ... which you can change to better suit your Rails implementation. None of this is cast in stone. I'm just reporting what these adapters do as delivered.

You can ignore the assembly name (", Todo-Angular") in the $type value; Breeze doesn't care about it.

Notice that the deleted "Cheese" entity was returned with all of its contents. I bet you don't have to do that. You could get away with returning a stripped down version that simply lets the client know Rails got the message:

{
    "$id": "2",
    "$type": "Todo.Models.TodoItem, Todo-Angular",
    "Id": 5
},

And now ... the complete JSON response body:

{
  "$id": "1",
  "$type": "Breeze.ContextProvider.SaveResult, Breeze.ContextProvider",
  "Entities": [
    {
      "$id": "2",
      "$type": "Todo.Models.TodoItem, Todo-Angular",
      "Id": 5,
      "Description": "Cheese",
      "CreatedAt": "2012-08-22T09:05:00.000Z",
      "IsDone": true,
      "IsArchived": false
    },
    {
      "$id": "3",
      "$type": "Todo.Models.TodoItem, Todo-Angular",
      "Id": 6,
      "Description": "Modified Todo",
      "CreatedAt": "2012-08-22T09:06:00.000Z",
      "IsDone": false,
      "IsArchived": false
    },
    {
      "$id": "4",
      "$type": "Todo.Models.TodoItem, Todo-Angular",
      "Id": 7,
      "Description": "New Todo",
      "CreatedAt": "2014-05-02T17:34:00.904Z",
      "IsDone": false,
      "IsArchived": false
    }
  ],
  "KeyMappings": [
    {
      "$id": "5",
      "$type": "Breeze.ContextProvider.KeyMapping, Breeze.ContextProvider",
      "EntityTypeName": "Todo.Models.TodoItem",
      "TempValue": -1,
      "RealValue": 7
    }
  ],
  "Errors": null
}