简体   繁体   中英

ember-data making extra GET requests for belongsTo relations

My model data is being properly serialized and deserialized, but it ends up making extra requests for data that is already present, side-loaded in the JSON document. (Those requests fail because we don't have an endpoint for those objects)

For example:

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

I'm using Rails and ember-data. On the rails side, my serializer looks like this:

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

My ember-data model looks like this:

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'),
});

And the ember serializer:

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

I tried adding {async: false} to the relations, but it had no effect.

Ember.Validations.Mixin is causing the rogue requests! I need to take it out or patch it.

However, I've noticed the ember-validations doesn't explicitly make any server calls - so the problem could still be in ember-data. One issue is that ember-validations attempts to validate the models before they have their relations loaded because it fires on init.

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

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