2

我正在尝试使用 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,store应该是自动设置的,但始终为null。

如何定义模型,以便商店可用?我错过了什么?

更新 1: 更新了 ember。现在我使用以下版本:

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
4

1 回答 1

0

主题应该是小写,另外 findAll 是一个内部方法,你应该使用不带附加参数的 find(然后调用 findAll)。

Cube.IndexRoute = Ember.Route.extend({
  model: function (params) {
    var store = this.get('store');
    return store.find('subject');
  }
});
于 2013-10-21T21:36:55.420 回答