我正在尝试使用ember数据从我的rails后端获取json数据。这是app.js
App.ApplicationAdapter = DS.RESTAdapter.extend({
host: 'http://localhost:3000',
buildURL: function(record, suffix) {
console.log(this._super(record, suffix) +'.json');
return this._super(record, suffix) + '.json';
}
});
App.Router.map(function() {
this.resource('posts', { path: '/' }, function() {
this.resource('post', { path: ':post_id' });
});
});
App.PostRoute = Ember.Route.extend({
model: function(params) {
return this.store.find('post', params.post_id);
}
});
App.PostsRoute = Ember.Route.extend({
model: function() {
return this.store.find('post');
}
});
App.Post = Ember.Object.extend({
title: DS.attr('string'),
body: DS.attr('string'),
created_at: DS.attr('date'),
updated_at: DS.attr('date')
});
这是/posts.json
返回的数据{
"posts":[
{
"id": 1,
"title": "Some Title",
"body": "Some text",
"created_at": "2014-05-04T22:25:35.578Z",
"updated_at": "2014-05-04T22:25:35.578Z"
},
{
"id": 2,
"title": "Some Title",
"body": "Some text",
"created_at": "2014-05-04T22:25:35.578Z",
"updated_at": "2014-05-04T22:25:35.578Z"
}
]
}
this.store.find('post')似乎不起作用,但我不确定为什么。在查看了文档和一些教程之后,我没有看到代码的问题。这是我得到的错误。
Error while loading route: TypeError: undefined is not a function
at Ember.Object.extend.applyTransforms (http://localhost:3000/assets/ember-data.min.js:9:15567)
at Ember.Object.extend.normalize (http://localhost:3000/assets/ember-data.min.js:9:15707)
at superFunction [as _super] (http://localhost:3000/assets/ember-1.5.1.js:7724:16)
at d.extend.normalize (http://localhost:3000/assets/ember-data.min.js:9:18220)
at superWrapper [as normalize] (http://localhost:3000/assets/ember-1.5.1.js:1293:16)
at null.<anonymous> (http://localhost:3000/assets/ember-data.min.js:9:19505)
at Array.map (native)
at d.extend.extractArray (http://localhost:3000/assets/ember-data.min.js:9:19474)
at superWrapper (http://localhost:3000/assets/ember-1.5.1.js:1293:16)
at Ember.Object.extend.extractFindAll (http://localhost:3000/assets/ember-data.min.js:9:16821)
答案 0 :(得分:4)
你的模型正在扩展错误的对象,我没有看到任何其他明显的错误。
App.Post = Ember.Object.extend({
title: DS.attr('string'),
body: DS.attr('string'),
created_at: DS.attr('date'),
updated_at: DS.attr('date')
});
App.Post = DS.Model.extend({
title: DS.attr('string'),
body: DS.attr('string'),
created_at: DS.attr('date'),
updated_at: DS.attr('date')
});