我正在尝试使用ember.js和ember-data为REST服务编写前端。服务器返回数据(我确实使用fiddler看到这个),但我总是得到错误Unable to set property 'store' of undefined or null reference
。我的JS代码:
window.Cube = Ember.Application.create({
LOG_TRANSITIONS: true,
LOG_TRANSITIONS_INTERNAL: true
});
var attr = DS.attr;
Cube.Subject = DS.Model.extend({
name: attr(),
change_date: attr(),
create_date: attr()
});
Cube.ApplicationAdapter = DS.RESTAdapter.extend({
namespace: 'backend/v1/api',
host: 'http://localhost:58721'
});
Cube.Store = DS.Store.extend({
revision: 12,
url: "http://localhost:58721",
adapter: Cube.ApplicationAdapter
});
Cube.IndexRoute = Ember.Route.extend({
model: function (params) {
var store = this.get('store');
return store.findAll('Subject');
}
});
错误源自ember-data.js:
modelFor: function(key) {
if (typeof key !== 'string') {
return key;
}
var factory = this.container.lookupFactory('model:'+key);
Ember.assert("No model was found for '" + key + "'", factory);
factory.store = this; // error here
factory.typeKey = key;
return factory;
}
据我了解ember,应该自动设置商店,但它总是为空。
如何定义模型,商店是否可用?我错过了什么?
更新1: 更新了余烬。现在我使用以下版本:
DEBUG: Ember : 1.1.0
DEBUG: Ember Data : 1.0.0-beta.3
DEBUG: Handlebars : 1.0.0
DEBUG: jQuery : 1.9.1
否我在控制台中收到以下错误:
No model was found for 'nextObject'
Error while loading route: TypeError: Unable to set property 'store' of undefined or null reference
答案 0 :(得分:0)
主题应该是小写,另外findAll是一个内部方法,你应该使用find而没有其他参数(然后调用findAll)。
Cube.IndexRoute = Ember.Route.extend({
model: function (params) {
var store = this.get('store');
return store.find('subject');
}
});