I'm using Ember CLI and by default it seems to set Ember.MODEL_FACTORY_INJECTIONS = true;
in app.js
.
I tried commenting this line out (and setting it to false) and then my app seemed to behave in some sort of strict mode. I got a bunch of failed assertions because some of my Model relations didn't explicitly specify the inverse.
This is the exact error:
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
Using the default Ember CLI generated app with Ember.MODEL_FACTORY_INJECTIONS = true;
, I didn't get these errors. So I'm lead to believe that this flag changes core behaviour somehow.
Insight please!
DS.Model
, your model base class, is just another class defined by Ember Data. In order for it to have special functionality like dependency injection, Ember needs to hook into that class so that when you instantiate it, the instance references the app container. When Ember.MODEL_FACTORY_INJECTIONS
is on, Ember applies additional mixins to the class so it can.
You can use Ember.getOwner(instance)
to get the app container (or owner) of an instance. This is how you can, for example, look up the store of your application or a controller instance. When you call MyModelClass.create()
directly, the owner isn't set. You either need to use Ember.setOwner()
or instantiate it with an existing owner. See ApplicationInstance#ownerInjection().
let owner = Ember.getOwner(this);
User.create(
owner.ownerInjection(),
{ username: 'rwjblue' }
)
However, many applications that use Ember Data don't instantiate model instances directly and don't need to do this.
You can currently use Ember.MODEL_FACTORY_INJECTIONS = true
to opt in to the functionality, but I believe they're moving towards making that the default in the future.
So turning the feature off caused errors since certain mixins were not getting included in your model classes.