为多级JSON创建breeze元数据

时间:2015-10-22 01:30:18

标签: javascript json metadata breeze

我是breeze的新手,我不太确定如何为服务器返回的这个JSON数据创建javascript元数据。我必须创建创建自定义元数据,因为服务器不提供我需要的元数据。我们使用oData。有人可以帮我创建基于此JSON数据的JavaScript元数据吗?



{
	id: "5091bed7-e035-45c6-b2c5-d1301e4170e6",
	name: "Mike",
	address: {
		city: "San Jose",
		state: "CA"
	},
	orders: [
		{
			id: "1889e425-e8ad-4b79-b797-fe9aa6b0e433",
			date: "2015/04/12 00:00:00",
			items: [
				{
					id: "a71aa2eb-9f8d-4234-aa83-6dfc5cd8abcc",
					description: "chocolate",
					price: "5.00"
				},
				{
					id: "5091bed7-e035-45c6-b2c5-d1301e414444",
					description: "cookie",
					price: "1.00"
				}
			]
		},
		{
			id: "5ed077b0-72c4-43d0-fe96-6bebce6e3c86",
			date: "2015/05/22 00:00:00",
			items: [
				{
					id: "bc5ed003-1302-4686-8294-4a0186e550a2",
					description: "pencil",
					price: "0.50"
				}
			]
		}
	]
}




1 个答案:

答案 0 :(得分:1)

在Breeze中,每个对象都将被处理为Entity(如果它有标识符)或Complex Type(如果没有)。在您的情况下,我已经为“客户”(顶级对象),“订单”和“OrderItem”创建了Entites。我为“地址”做了一个ComplexType。

请注意,每个传入对象都应该有一个“$ type”属性来告诉Breeze它是什么实体类型或复杂类型。没有它,你需要一个自定义[JsonResultsAdapter]告诉Breeze类型。

我的元数据与您的对象的不同之处在于我假设外键可用于将Customer连接到Order到OrderItem,反之亦然。这使得实体更易于使用,但如果您的服务器无法提供,则应从元数据中省略它们。

{
  "structuralTypes": [
    {
      "shortName": "Address",
      "namespace": "MyApp",
      "isComplexType": true,
      "dataProperties": [
        {
          "name": "city",
          "dataType": "String",
          "maxLength": 15
        },
        {
          "name": "state",
          "dataType": "String",
          "maxLength": 15
        }
      ]
    },
    {
      "shortName": "Customer",
      "namespace": "MyApp",
      "autoGeneratedKeyType": "KeyGenerator",
      "defaultResourceName": "Customers",
      "dataProperties": [
        {
          "name": "id",
          "dataType": "Guid",
          "isNullable": false,
          "isPartOfKey": true
        },
        {
          "name": "name",
          "dataType": "String",
          "maxLength": 30
        },
        {
          "name": "location",
          "complexTypeName": "Address:#MyApp"
        }
      ],
      "navigationProperties": [
        {
          "name": "orders",
          "entityTypeName": "Order:#MyApp",
          "isScalar": false,
          "associationName": "AN_Customer_Order"
        }
      ]
    },
    {
      "shortName": "Order",
      "namespace": "MyApp",
      "autoGeneratedKeyType": "Identity",
      "defaultResourceName": "Orders",
      "dataProperties": [
        {
          "name": "id",
          "dataType": "Guid",
          "isNullable": false,
          "isPartOfKey": true
        },
        {
          "name": "customerID",
          "dataType": "Guid"
        },
        {
          "name": "date",
          "dataType": "DateTime"
        }
      ],
      "navigationProperties": [
        {
          "name": "customer",
          "entityTypeName": "Customer:#MyApp",
          "isScalar": true,
          "associationName": "AN_Customer_Order",
          "foreignKeyNames": [
            "customerID"
          ]
        },
        {
          "name": "items",
          "entityTypeName": "OrderItem:#MyApp",
          "isScalar": false,
          "associationName": "AN_Order_OrderItem"
        }
      ]
    },
    {
      "shortName": "OrderItem",
      "namespace": "MyApp",
      "autoGeneratedKeyType": "None",
      "defaultResourceName": "OrderItems",
      "dataProperties": [
        {
          "name": "id",
          "dataType": "Guid",
          "isNullable": false,
          "isPartOfKey": true
        },
        {
          "name": "orderID",
          "dataType": "Guid",
          "isNullable": false
        },
        {
          "name": "description",
          "dataType": "String",
          "isNullable": false
        },
        {
          "name": "price",
          "dataType": "Decimal",
          "isNullable": false
        }
      ],
      "navigationProperties": [
        {
          "name": "order",
          "entityTypeName": "Order:#MyApp",
          "isScalar": true,
          "associationName": "AN_Order_OrderItem",
          "foreignKeyNames": [
            "orderID"
          ]
        }
      ]
    }
  ],
  "resourceEntityTypeMap": {
    "Customers": "Customer:#MyApp",
    "Orders": "Order:#MyApp",
    "OrderItems": "OrderItem:#MyApp"
  }
}