我的api api/customers
返回:
{
"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"
}
]
}
但是ember(/customers
)应该期待:
{
"customers": [
{
"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"
}
]
}
我找到了这个答案:GET unconventional JSON with Ember-data
所以我尝试(在我的app / serializers / customers.js中):
export default DS.RESTSerializer.extend({
extractArray: function(store, type, payload, id) {
var newpayload = { customers: payload.value };
return this._super(store, type, newpayload, id);
},
});
并在app / routes / customers.js
中export default Ember.Route.extend({
model: function() {
return this.store.find('customer');
}
});
app / models / customer.js中的
export default DS.Model.extend({
CustomerID: DS.attr('string'),
CompanyName: DS.attr('string'),
ContactName: DS.attr('string'),
ContactTitle: DS.attr('string'),
});
并在app / templates / customers.hbs中:
{{#each}}
{{CustomerID}}({{ContactName}}
{{/each}}
我打开浏览器,但它是空的,没有错误信息,那为什么它不起作用?
答案 0 :(得分:2)
在序列化程序中覆盖typeForRoot
:
typeForRoot: function(root) {
if (root === 'value') root = 'customers';
return this._super(root);
}
这里的问题是" root" key(Jmber中的顶级键,Ember Data用来确定这个模型应该是什么)是错误的,所以你需要typeForRoot
来修补它。
(extractArray
执行不同的操作:它处理数组值属性。无论如何,它无法修复app/serializers/customers.js
(可能应该是{{} 1}})因为即使知道涉及客户模型,Ember目前还没办法。)
答案 1 :(得分:0)
我会在JavaScript中这样做
var apiCustomers = {
"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"
}]
};
var emberCustomers = {"customers": []};
emberCustomers.customers = apiCustomers.value;
console.log(JSON.stringify(emberCustomers));
如果您正在使用此类内容获取JSON,请点击此处。
App.ApplicationRoute = Ember.Route.extend({
model: function() {
return Ember.$.getJSON('https://api.github.com/repos/emberjs/ember.js/pulls').then(function(data) {
var emberCustomers = {"customers": []};
emberCustomers.customers = data.value;
return emberCustomers;
});
}
});