ember 的 REST Adapter 期待的 json (customer/:customer_id)是下面这样的格式的:
{
"customer": {
"CustomerID": "ALFKI",
"CompanyName": "Alfreds Futterkiste",
"ContactName": "Maria Anders",
"ContactTitle": "Sales Representative",
"id": "b0d16ed0-c901-4ca3-ba41-7fc74c96909f"
}
}
但是我的服务器的 API 是这样的:
{
"CustomerID": "ALFKI",
"CompanyName": "Alfreds Futterkiste",
"ContactName": "Maria Anders",
"ContactTitle": "Sales Representative",
"id": "b0d16ed0-c901-4ca3-ba41-7fc74c96909f"
}
于是我可以这样(在 app/serializers/customer.js 文件里):
serializeIntoHash: function(data, type, record, options) {
Ember.merge(data, this.serialize(record, options));
}
这样就解决问题了。
但是另外一个问题是,我的服务器返回所有 coustomers 的 API 是这样的:
{
"value": [
{
"CustomerID": "ALFKI",
"CompanyName": "Alfreds Futterkiste",
"ContactName": "Maria Anders",
"ContactTitle": "Sales Representative",
"id": "b0d16ed0-c901-4ca3-ba41-7fc74c96909f"
},
{
"CustomerID": "ANATR",
"CompanyName": "Ana Trujillo Emparedados y helados",
"ContactName": "Ana Trujillo",
"ContactTitle": "Owner",
"id": "3f8ac226-9f78-42df-b337-0505f69792c3"
},
{
"CustomerID": "ANTON",
"CompanyName": "Antonio Moreno Taquería",
"ContactName": "Antonio Moreno",
"ContactTitle": "Owner",
"id": "09d31df6-69f4-43e4-9cc6-7faa5b8b5e3b"
}]
}
我怎样像上面那个 customer/:customer_id
例子一样,把返回所有 customers 的 API
变成 ember 的 /customers
所期待的那样呢?
在你的app/serializers/customer.js文件中创建下面的方法