ember-model
ember-model copied to clipboard
Ember-cli, declaring an association fails because the model's container is not defined
Hello there !
I have been reading all the other issues about ES6, containers, belongsTo and hasMany failures but my problem is not exactly the same, thus me posting here.
TL;DR: When records are instanciated, even through the store, they have no container, thus failing to fetch associations.
When I declare an association :
import Ember from "ember";
Model = Ember.Model.extend({
// ...
user: Ember.belongsTo("user", { key: 'user_id' })
});
export default Model;
import Ember from "ember";
User = Ember.Model.extend({
// ...
});
export default User;
And fetching a record for instance :
// Route
import Ember from "ember";
export default Ember.Route.extend({
model: function() {
return this.get('store').modelFor('myModel').find(2);
},
setupController: function(controller, model) {
controller.set('model', model);
}
});
When I try to access my association like doing : controller.get('model').get('user')
it will fail with : *Uncaught TypeError: Cannot read property 'modelFor' of null *
The error is in belongs_to.js#L20
But it really fails on belongs_to.js#L5 because record.container
is undefined
.
I think I read that the store was supposed to assign a container to newly created records but it seems it is not happening.
My workaround so far is to declare an initializer :
import Ember from "ember";
export default {
name: 'model-container',
initialize: function(container, app) {
/* Needed for new records to have a container defined */
Ember.Model.reopen({
init: function() {
this.container = container;
this._super();
}
});
}
};
But I am actually not sure if it will work correctly (not sure if the container
is really the same that the one accessible in the store ?)
Thanks for your help :)