获取所有文章是可以的,但是当尝试通过路由url直接检索一篇文章时,系统错误-未定义。
var articles = Ember.$.getJSON('http://localhost/emberdata/api/articles');将返回:
[{"articles":
[
{
"id":1,
"title":"Ember is the best",
"author":"brk","excerpt":"Programming is awesome"
},
{
"id":2,
"title":"Backbone not a framework",
"author":"krb",
"excerpt":"Server-side controller sucks"
},
{
"id":3,
"title":"Javascript pwned",
"author":"rbk",
"excerpt":"I know right"
}
]
}]API是使用PHP创建的
此路径工作查找,显示车把模板中的所有数据
App.ArticlesRoute = Ember.Route.extend({
model: function() {
return articles;
}
});但是,对于子视图路由,将返回未定义的
App.ArticleRoute = Ember.Route.extend({
model: function(params) {
var article = articles.findBy('id', params.article_id);
console.log(article);
return article;
}
});直接调用子视图URL不起作用:
http://localhost/emberdata/#/articles/1然而,单击文章的链接,这些子视图路由可以工作:
this.resource('article', { path: ':article_id' });这是一个错误:

发布于 2014-08-24 15:35:56
Ember.$.getJSON()将返回一个承诺(参见:http://emberjs.com/api/classes/Ember.PromiseProxyMixin.html)。不能根据承诺调用findBy()方法。
话虽如此,你却让你自己变得很困难。我建议开始使用DS.RESTAdapter。在你的例子中,它应该是这样的:
App.ApplicationAdapter = DS.RESTAdapter.extend({
namespace: 'emberdata/api'
});然后清除(或删除) ArticlesRoute,因为您将使用约定(而不是配置):
App.ArticlesRoute = Ember.Route.extend({
}); ArticleRoute的调制解调器。但是,如果后端不支持/emberdata/api/code/1这样的调用,则使用以下代码:
App.ArticleRoute = Ember.Route.extend({
model: function(params) {
var article = this.store.find('article').findBy('id', params.article_id);
console.log(article);
return article;
}
});https://stackoverflow.com/questions/25472931
复制相似问题