ebryn / ember-model

A lightweight model library for Ember.js
MIT License
524 stars 159 forks source link

Ember.Model.Store `container` attribute triggers `Cannot read property '__container__' of undefined` on Ember 2.13 production builds #457

Closed TRMW closed 6 years ago

TRMW commented 7 years ago

(I'm mostly just submitting this so future people will find something when Googling for this error.)

Production (but not dev) builds of Ember 2.13 seem to error out before anything is rendered with this error:

TypeError: Cannot read property '__container__' of undefined

I don't entirely understand why this is happening, but seems related to this Ember issue:

emberjs/ember.js#15322

It happens when Ember goes to look up the container injections for the route type, sees store:main (registered by Ember Model), and tries to initialize the Ember.Model.Store object for the first time. It then gets into this block and tries to do Ember.getOwner(this).__container__ (I believe this is the store here) where getOwner returns undefined:

var INJECTED_DEPRECATED_CONTAINER_DESC = {
  // stuff...
  get: function () {
    _emberDebug.deprecate('Using the injected `container` is deprecated. Please use the `getOwner` helper instead to access the owner of this object.', false, { id: 'ember-application.injected-container', until: '2.13.0', url: 'http://emberjs.com/deprecations/v2.x#toc_injected-container-access' });
    return this[CONTAINER_OVERRIDE] || _emberUtils.getOwner(this).__container__;
  },
  // more stuff...
}

I hacked around it on our custom fork of Ember Model by just commenting out the container: null line here:

Ember.Model.Store = Ember.Object.extend({
  // Prod builds (but not dev builds) of Ember 2.13 seem to blow up if you
  // define an attribute called `container`.
  // See https://github.com/emberjs/ember.js/issues/15322
  // container: null,

I've verified that fixes the error.

There might be a more correct fix for this that involved making Ember Model work correctly with the new getOwner() container API, or maybe Ember needs to fix the linked issue.

spectras commented 7 years ago

The only correct way forward is to use the Ember.getOwner() API. Right now, the workaround only works because Ember maintains backwards compatibility. If one looks at his debug console, one should see deprecation warnings, though.

GavinJoyce commented 6 years ago

https://github.com/ebryn/ember-model/releases/tag/v2.18.0