ember-data为belongsTo关系提供额外的GET请求

时间:2014-09-02 19:29:43

标签: ruby-on-rails json ember.js ember-data

我的模型数据正在被正确地序列化和反序列化,但它最终会对已存在的数据进行额外请求,并在JSON文档中侧载。 (这些请求失败,因为我们没有这些对象的端点)

例如:

GET http://localhost:3000/api/2/apps/53fde960cc095d0e0000002d/in_app_message_buttons/5405c098cc095d88e800000b 404 (Not Found) 

我使用Rails和ember-data。在轨道方面,我的序列化器看起来像这样:

class PopUpMessageVariationSerializer < ActiveModel::Serializer
  embed :ids, include: true  #model not properly deserialized till we added this
  has_one     :cta_button, :root => :in_app_message_button
  has_one     :cancel_button,  :root => :in_app_message_button
  has_one :headline, :root => :message_labels
  has_one :body, :root => :message_labels
end

我的余烬数据模型如下所示:

PopUpMessageVariation = InAppMessageVariation.extend(Ember.Validations.Mixin, {
 ctaButton: DS.belongsTo('in_app_message_button'),
 cancelButton: DS.belongsTo('in_app_message_button'),
 headline: DS.belongsTo('message_label'),
    body: DS.belongsTo('message_label'),
});

和余烬序列化器:

PopUpMessageVariationSerializer = DS.ActiveModelSerializer.extend(DS.EmbeddedRecordsMixin, {
    attrs: {
      headline: { embedded: 'always' },
      body: { embedded: 'always' },
      ctaButton: { embedded: 'always' },
      cancelButton: { embedded: 'always' }
    }
});

我尝试将{async:false}添加到关系中,但它没有效果。

1 个答案:

答案 0 :(得分:0)

Ember.Validations.Mixin导致了恶意请求!我需要拿出来或修补它。

但是,我注意到ember-validations没有明确地进行任何服务器调用 - 所以问题仍然可能在于ember-data。一个问题是,ember-validations尝试在加载关系之前验证模型,因为它在init上触发。

Locally I've patched ember-validations to prevent it from firing on init as described in this github issue.