Ember.MODEL_FACTORY_INJECTIONS究竟做了什么?

Jam*_*ong 4 ember.js ember-data ember-cli

我使用的灰烬CLI默认情况下它似乎设置Ember.MODEL_FACTORY_INJECTIONS = true;app.js.

我尝试将此行注释掉(并将其设置为false)然后我的应用程序似乎表现出某种严格的模式.我得到了一堆失败的断言,因为我的一些模型关系没有明确指定.

这是确切的错误:

You defined the 'account' relationship on (subclass of DS.Model), but multiple possible inverse relationships of type (subclass of DS.Model) were found on (subclass of DS.Model). Look at http://emberjs.com/guides/models/defining-models/#toc_explicit-inverses for how to explicitly specify inverses

使用默认的Ember CLI生成的应用程序Ember.MODEL_FACTORY_INJECTIONS = true;,我没有收到这些错误.所以我会相信这个标志会以某种方式改变核心行为.

洞察力!

Jon*_*ran 5

DS.Model,您的模型基类,只是Ember Data定义的另一个类.为了使它具有依赖注入等特殊功能,Ember需要挂钩到该类,以便在实例化时,实例引用应用程序容器.当Ember.MODEL_FACTORY_INJECTIONS打开时,Ember将其他混音应用于该类,以便它可以.

您可以使用它Ember.getOwner(instance)来获取实例的应用容器(或所有者).例如,您可以查看应用程序的存储或控制器实例.MyModelClass.create()直接呼叫时,未设置所有者.您需要使用Ember.setOwner()现有所有者或将其实例化.请参见ApplicationInstance#ownerInjection().

let owner = Ember.getOwner(this);

User.create(
  owner.ownerInjection(),
  { username: 'rwjblue' }
)
Run Code Online (Sandbox Code Playgroud)

但是,许多使用Ember Data的应用程序不直接实例化模型实例,也不需要这样做.

您目前Ember.MODEL_FACTORY_INJECTIONS = true可以选择使用该功能,但我相信他们将来会将其作为默认设置.

因此关闭该功能会导致错误,因为某些mixin未包含在您的模型类中.