我用ajax拉了一些JSON(因为我无法通过我有限的理解/缺乏RESTAdapter的有用示例...... 来通过CORS) - 和我得到了这些嵌套对象。我需要一本书' - 但我不确定如何格式化这个JSON以便它可读。
import Ember from 'ember';
import ajax from 'ic-ajax';
var libraryThingApi = "http://www.librarything.com/api_getdata.php?";
export default Ember.Route.extend({
model: function() {
var libraryData = ajax({
url: libraryThingApi + "userid=F.L.O.W.",
type: 'GET',
dataType: 'jsonp'
});
console.log(libraryData);
return libraryData;
}
});
Promise
_id: 47
_label: "ic-ajax: unwrap raw ajax response"
...
_result: Object
books: Object
111333266: Object
title: "a book title"
111730480: Object
title: "a book title"
settings: Object
theuser: "F.L.O.W."
more_things: "etc"
{
books: {
111601539: {
book_id: "111601539",
title: "Rosie Revere, Engineer",
author_fl: "Andrea Beaty",
copies: 1
},
121604536: {
book_id: "121604536",
title: "Ember for people who aren't core.",
author_fl: "No author",
copies: "This book does not exist"
}
},
settings: {
theuser: "sheriffderek"
}
}
答案 0 :(得分:0)
因为你只是抓住原始的json,你可以真正地使用它,不管你喜欢什么。然后,这就变成了您想要如何访问路线模板中的数据的问题。
Ember的模型钩子是承诺意识,所以你只需返回承诺是正确的。那个承诺的实现价值就是你对模板感兴趣的东西。因此,如果满足的值是这样的:
{
books: [
{id: 1, title: "some title", nested_data: {author: "some author"}},
{id: 2, title: "another title", nested_data: {author: "another author"}},
]
}
然后在您的路线模板中,您可以从模型钩子中返回的承诺中访问已实现的值。 Pre Ember 1.10(在1.x系列中稍后弃用)你可以在你的路线模板中通过你的书籍,如下所示:
<ul>
{{#each book in model.books}}
<li>{{book.title}} by {{book.nested_data.author}}</li>
{{/each}}
</ul>
Ember 1.10及更高版本为每个陈述引入了块参数:
<ul>
{{#each model.books as |book|}}
<li>{{book.title}} by {{book.nested_data.author}}</li>
{{/each}}
</ul>
阅读有关Ember 1.10版本here的信息。