mercredi 30 mars 2016

Ember data dependent keys undefined

A lot of the other posts on this topic are 2+ years old, so here goes a potentially simple question.

I am using Ember data relationships to have a 'bizinfo' record belong to a 'user' record. Seems simple, but I am having the worst time of it.

In app/models/bizinfo.js I have the line:

'ownedBy': DS.belongsTo('user')

And in my route, where I validate and then save the model, I have the following code:

user_id: Ember.computed(function(){
    return `${this.get('session.data.authenticated.user_id')}`;
  }),

  user: Ember.computed(function(){
    return this.store.findRecord('user', this.get('user_id'));
  }),

  model(params){
    return this.store.createRecord('bizinfo', {'ownedBy': this.get('user')});
  },

at this point if I go into the Ember inspector to look at the 'bizinfo' data object, I see the following under the belongsTo tab:

ownedBy : <(subclass of Ember.ObjectProxy):ember1053>

Here is the code from my submit action:

submit() {
  let model = this.currentModel;
  console.log(model.ownedBy);
  console.log(`what does the model look like?`);
  console.log(model.toJSON());
  model.validate().then(({model, validations}) => {
    if (validations.get('isValid')) {
      this.setProperties({
        showAlert: false,
        isRegistered: true,
        showCode: false
      });
      let success = (response) => {
        console.log(`Server responded with ${response.toJSON()}`);
      };

      let failure = (response) => {
        console.log(`Server responded with ${response}`);
      };
      model.save().then(success, failure);
    } else {
      this.set('showAlert', true);
    }
    this.set('didValidate', true);
  }, (errors) => {
    console.log(`errors from failed validation: ${errors}`);
  });
},

So here is the result of the first console.log statement:

ComputedProperty {isDescriptor: true, _dependentKeys: undefined, _suspended: undefined, _meta: Object, _volatile: false…}

And when I look at the model.toJSON() log, I see

ownedBy: null

Can anyone see what's going wrong here? Is it the create record statement? I have tried a lot of different permutations (such as submitting just the id as the 'user' parameter.




Aucun commentaire:

Enregistrer un commentaire