为什么我得到一个空的记录存储区?
Why I'm getting an empty store of records?
我有后端服务这个json
{
"bars": [
{
"id": 1,
"name": "one bar"
},
{
"id": 2,
"name": "second bar"
},
{
"id": 3,
"name": "third bar"
}
]
}
在 host: 'http://localhost:3000/bars'
这就是我在客户端的所有内容:
;(function(Ember, DS) {
var App = Ember.Application.create({
LOG_TRANSITIONS: true
});
window.App= App;
App.ApplicationAdapter = DS.RESTAdapter.extend({
host: 'http://localhost:3000'
});
App.ApplicationSerializer = DS.JSONSerializer.extend({
primaryKey: 'id'
});
App.Bar = DS.Model.extend({
name: DS.attr('string')
});
App.ApplicationRoute = Ember.Route.extend({
model: function() {
var bars = this.store.find('bar');
bars.then(function(bars) {
console.log(bars.content.length); // return 0 where the xhr request getting 3 items
});
return bars;
}
});
}(Ember, DS));
我正在使用
Ember : 1.9.0
Ember Data : 1.0.0-beta.14.1
Handlebars : 2.0.0
jQuery : 2.1.1
PS: jquery ajax 请求似乎正常工作:
XHR finished loading: GET "http://localhost:3000/bars".
你用错了Serializer
注释掉 JSONSerializer
并改为激活 ActiveModelSerializer
,您应该可以开始了
// App.ApplicationSerializer = DS.JSONSerializer.extend({
// primaryKey: 'id'
// });
App.ApplicationSerializer = DS.ActiveModelSerializer.extend({
primaryKey: 'id'
});
工作解决方案here
我有后端服务这个json
{
"bars": [
{
"id": 1,
"name": "one bar"
},
{
"id": 2,
"name": "second bar"
},
{
"id": 3,
"name": "third bar"
}
]
}
在 host: 'http://localhost:3000/bars'
这就是我在客户端的所有内容:
;(function(Ember, DS) {
var App = Ember.Application.create({
LOG_TRANSITIONS: true
});
window.App= App;
App.ApplicationAdapter = DS.RESTAdapter.extend({
host: 'http://localhost:3000'
});
App.ApplicationSerializer = DS.JSONSerializer.extend({
primaryKey: 'id'
});
App.Bar = DS.Model.extend({
name: DS.attr('string')
});
App.ApplicationRoute = Ember.Route.extend({
model: function() {
var bars = this.store.find('bar');
bars.then(function(bars) {
console.log(bars.content.length); // return 0 where the xhr request getting 3 items
});
return bars;
}
});
}(Ember, DS));
我正在使用
Ember : 1.9.0
Ember Data : 1.0.0-beta.14.1
Handlebars : 2.0.0
jQuery : 2.1.1
PS: jquery ajax 请求似乎正常工作:
XHR finished loading: GET "http://localhost:3000/bars".
你用错了Serializer
注释掉 JSONSerializer
并改为激活 ActiveModelSerializer
,您应该可以开始了
// App.ApplicationSerializer = DS.JSONSerializer.extend({
// primaryKey: 'id'
// });
App.ApplicationSerializer = DS.ActiveModelSerializer.extend({
primaryKey: 'id'
});
工作解决方案here