Ember数据剥离有很多关系

时间:2014-08-05 04:14:59

标签: ember.js ember-data

我正在度过一段可怕的时光,但我在控制器上有以下动作:

saveOrganization: function() {  
  var org = this.get('model');
  var users = org.get('users').then(function() {
    console.log(org); //users are a part of org
    org.save(); //users are not sent to server
  });
}

及相关组织模式:

export default DS.Model.extend({
  createdAt: DS.attr('date'),
  updatedAt: DS.attr('date'),
  name: DS.attr('string'),
  address: DS.attr('string'),
  city: DS.attr('string'),
  state: DS.attr('string'),
  zip: DS.attr('string'),
  users: DS.hasMany('user',{async:true}),
  totalUsers: function() {
    return this.get('users').get('length');
  }.property('users.@each')
});

和关联用户模型:

export default DS.Model.extend({
    createdAt: DS.attr('date'),
    updatedAt: DS.attr('date'),
    name: DS.attr('string'),
    email: DS.attr('string'),
    bio: DS.attr('string'),
    picture: DS.attr('string'),
    organization: DS.belongsTo('organization'),
    type: DS.attr('string'),
    username: DS.attr('string'),
    password: DS.attr('string')
});

从评论中可以看出,当我将users数组作为组织的一部分时,我可以看到数据,但是当我保存数据时,ember数据永远不会将用户数组作为发送到数据的数据的一部分发送给服务器

任何人都可以帮我弄清楚可能发生的事情吗?

感谢。

1 个答案:

答案 0 :(得分:1)

堆叠过流不会让我把它标记为Serialize ids when saving emberjs model的欺骗,但它确实是。 Ember Data在这方面是愚蠢的,如果它是ManyToOne关系,它只包含来自belongsTo方的id。

https://github.com/emberjs/data/commit/7f752ad15eb9b9454e3da3f4e0b8c487cdc70ff0#commitcomment-4923439

App.ApplicationSerializer = DS.RESTSerializer.extend({

 serializeHasMany: function(record, json, relationship) {
    var key = relationship.key;
    var payloadKey = this.keyForRelationship ? this.keyForRelationship(key, "hasMany") : key;
    var relationshipType = RelationshipChange.determineRelationshipType(record.constructor, relationship);

    if (relationshipType === 'manyToNone' || relationshipType === 'manyToMany'
        || relationshipType === 'manyToOne') {  // This is the change
      json[payloadKey] = get(record, key).mapBy('id');
      // TODO support for polymorphic manyToNone and manyToMany relationships
    }
  },
});