mardi 31 janvier 2017

Why does this Ember 2 store.createRecord('foo', {user: .., account: ..}) result in null user?

In this toy Ember 2 app, a user models/user.js and an account models/account.js have identical contents:

export default DS.Model.extend({
  name: DS.attr('string'),
});

The final foo models/foo.js belongs-to a user and to an account:

export default DS.Model.extend({
  user: DS.belongsTo({}),
  account: DS.belongsTo({}),
  name: DS.attr('string')
});

The router.js exposes creation of foo belonging to an account and a user:

this.route('accounts', function () {
  this.route('one', {path: '/:account_id'}, function () {
    this.route('foos', function () {
      this.route('new');
    });
  });
});

The account id will come from the route, however the user id will come from somewhere else.

So, a route accounts/one.js exposes the account model:

model(params) {
  return this.store.findRecord('account', params.account_id);
},

A route accounts/one/foos/new.js exposes the create-a-new foo model. It will attempt to find the corresponding user record for the id it knows:

model() {
  let userId = 2; // it comes from "somewhere else"
  return this.store.createRecord('idea', {
    user: this.store.findRecord('user', userId),
    account: this.modelFor('accounts.one')
  });
},

Why does the final resulting model have the correct account but null user?




Aucun commentaire:

Enregistrer un commentaire