简体   繁体   English

Ember.js(Ember数据):为什么同胞关系消失了?

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

I'm having a problem with my sibling hasMany relationships disappearing. 我的兄弟姐妹hasMany失去了很多关系。 Working with Ember data canary. 使用Ember数据金丝雀。

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: 通过以下方式删除了PaymentMethod后更新用户时:

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 [] . apps数组设置为空[] It happens the opposite way as well, deleteing or adding an app with a paymentMethod will unset the paymentMethod array. 情况也相反,删除或添加带有paymentMethod的应用程序将取消设置paymentMethod数组。

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. 您正在将hasMany关联命名为单数,但实际上并没有遵循约定。 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? 我想您的后端以某种方式将您限制在此有效载荷上?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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