简体   繁体   中英

Ember.js (Ember Data): Why are sibling relationships disappearing?

I'm having a problem with my sibling hasMany relationships disappearing. Working with Ember data canary.

I have the following data model:

import DS from 'ember-data';

export default DS.Model.extend({
  // More here, discarded for brevity...
  app: DS.hasMany('app', { async: true }),
  paymentMethod: DS.hasMany('paymentMethod', { async: true })
});

When user is updated after deleting a paymentMethod in the following way:

var paymentMethod = this.get('content'),
    currentUser   = this.session.get('currentUser.content');

currentUser.get('paymentMethod').then(function ( paymentMethods ) {

  paymentMethods.removeObject(paymentMethod.get('id'));

  paymentMethod.destroyRecord();

  currentUser.save();

}, handleError);

or saving in the following way:

var paymentMethod = self.store.createRecord('payment-method', paymentMethodData);

paymentMethod.save().then(function ( PaymentMethod ) {
  currentUser.get('paymentMethod').addObject(PaymentMethod);

  currentUser.save().then(function ( /* record */ ) {...

The apps array is set to an empty [] . It happens the opposite way as well, deleteing or adding an app with a paymentMethod will unset the paymentMethod array.

I have the following serializer in place, but it appears as the relationship is set as an empty array before the record gets to the serializer:

var json = {
  _id:        user.get('id'),
  name: {
    first:    user.get('firstName'),
    last:     user.get('lastName'),
    company:  user.get('companyName')
  },
  login: {
    email:    user.get('email'),
    password: user.get('password')
  },
  app:        user.get('app').mapProperty('id'),
  paymentMethod: user.get('paymentMethod').mapProperty('id'),
  time_stamp: user.get('time_stamp')
};

return json;

Sorry for the overload. Hope you can help.

You are naming your hasMany associations in singular, which isn't really following the convention. That being said, you have no 'apps' array. I don't think that should cause you any problems, I am just pointing out because you maybe searching for the wrong thing.

I suppose your backend somehow restricts you to this payload?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM