简体   繁体   English

具有关系的Emberjs模型引发错误:“无法设置未定义的属性'store'”

[英]Emberjs models with relations throws an error: “Cannot set property 'store' of undefined”

I'm trying out ember at my work to see if we should use it for our future applications I am doing a simple test application and I wanted to try out the relations between the models. 我正在尝试工作中的余烬,看看是否应该在将来的应用程序中使用它。我正在做一个简单的测试应用程序,我想尝试模型之间的关系。 This is the code I have that defines the models: 这是我定义模型的代码:

var App = Ember.Application.create();

App.Router.map(function () {
    this.resource('index', {path: "/"}, function () {
        this.resource("config", {path: "/config/:config_id"});
    });
});

App.Store = DS.Store.extend();

App.Conf = DS.Model.extend({
    module : DS.attr(),
    reports: DS.hasMany('report'),
    isClean: function() {
        return !this.get('reports').isAny('isClean', false);
    }.property('reports.@each')
});


App.Report = DS.Model.extend({
    country: DS.attr(),
    google_account_id: DS.attr(),
    web_property_id: DS.attr(),
    custom_source_uid: DS.attr(),
    isClean: function() {
        return (
                this.get('country') != '' &&
                this.get('google_account_id') != '' &&
                this.get('web_property_id') != '' &&
                this.get('custom_source_uid') != ''
                );
    }.property('country', 'google_account_id', 'web_property_id', 'custom_source_uid')
});

App.ApplicationAdapter = DS.RESTAdapter.extend({
    host: 'http://playground.loc/battle_of_frameworks/json.php'
});

…and here is the JSON that is being loaded: …这是正在加载的JSON:

The error I get is: 我得到的错误是:

Error while loading route: TypeError: Cannot set property 'store' of undefined 加载路线时出错:TypeError:无法设置未定义的属性“ store”

I Googled the problem and it's usually something about naming your models in plural (ie: App.Reports) which I'm not doing. 我用Google搜索了这个问题,通常是将模型命名为复数形式(即App.Reports),而我没有这样做。 So I am not sure what the problem is here. 所以我不确定这是什么问题。 Can anyone give any insights? 谁能提供任何见解?

There are several problems in your code. 您的代码中有几个问题。

Your server doesn't provide the payload expected by Ember Data. 您的服务器未提供Ember Data所需的有效负载。 I would recommend reading this document about customizing your serializer if you can't generate the proper json payload with your backend. 如果您无法通过后端生成正确的json负载,我建议阅读有关自定义序列化程序的文档

Ember.js is all about convention over configuration. Ember.js完全是关于配置的约定。 Right now, you are not following those conventions: 目前,您没有遵循这些约定:

  • attributes are camelcased 属性是驼峰式的

     App.Report = DS.Model.extend({ googleAccountId: DS.attr() //instead of google_account_id }); 
  • you don't need to create the index route, it comes for free in Ember . 您无需创建索引路由, 它在Ember中免费提供 So your router should simply look like: 因此,您的路由器应该看起来像:

    App.Router.map(function () { this.resource("config", {path: "/config/:config_id"}); });

  • Are you sure that your backend expects the Config to be served from /config/:config_id and not /configs/:config_id ? 你确定你的后端预计该Config从送达/config/:config_id而不是/configs/:config_id

  • You declare a config resource. 您声明config资源。 The convention is to have a App.Config model and not App.Conf 约定是具有App.Config模型而不是App.Conf

In order to clean your code, you can also take advantage of computed properties to DRY your code: 为了清除您的代码,您还可以利用计算属性来使代码干燥:

App.Report = DS.Model.extend({
  country: DS.attr(),
  googleAccountId: DS.attr(),
  webPropertyId: DS.attr(),
  customSourceUid: DS.attr(),
  isClean: Ember.computed.and('country', 'googleAccountId', 'webPropertyId', 'customSourceUid')
});

You also need to pay attention when defining a computed property based on an array. 在基于数组定义计算属性时,还需要注意。 The isClean of Config uses isClean of Report but your computed property observes only the elements of your Report association. ConfigisClean使用Report isClean ,但是您的计算属性仅观察Report关联的元素。 The correct way of writing it is: 正确的编写方式是:

App.Config = DS.Model.extend({
  module : DS.attr(),
  reports: DS.hasMany('report'),
  isClean: function() {
    return !this.get('reports').isAny('isClean', false);
  }.property('reports.@each.isClean')  //make sure to invalidate your computed property when `isClean` changes
});

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM