我正在使用ajax提取一些JSON (因为我对RESTAdapter.的理解有限/缺乏有用的示例,无法通过CORS )--而且我得到了这些嵌套对象。我需要一个'book‘数组-但我不知道如何格式化这个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"
}
}发布于 2015-04-04 20:02:41
因为您只是在抓取原始的json,所以您可以任意使用它。然后,这就变成了您想要如何访问路由模板中的数据的问题。
恩伯的模型钩子是承诺知道,所以你是正确的,只是返回的承诺。该承诺的实现值就是您对模板感兴趣的内容。因此,如果实现的值如下所示:
{
books: [
{id: 1, title: "some title", nested_data: {author: "some author"}},
{id: 2, title: "another title", nested_data: {author: "another author"}},
]
}然后,在用于路由的模板中,您可以从模型钩子中返回的承诺中访问已实现的值。Pre 1.10 (在1.x系列后面不再推荐)您可以在您的路径模板中浏览您的书籍,如下所示:
<ul>
{{#each book in model.books}}
<li>{{book.title}} by {{book.nested_data.author}}</li>
{{/each}}
</ul>EMB1.10及以后的每一次发言都采用块对齐:
<ul>
{{#each model.books as |book|}}
<li>{{book.title}} by {{book.nested_data.author}}</li>
{{/each}}
</ul>阅读有关Ember1.10发布这里的信息。
https://stackoverflow.com/questions/29445263
复制相似问题