getoutreach / epf

A framework for keeping your Ember.js apps in sync.
http://epf.io
MIT License
369 stars 33 forks source link

serializerFor should only override typeKey if it doesn't already exist #132

Closed yaymukund closed 10 years ago

yaymukund commented 10 years ago

I have multiple ways of serializing data because of a legacy API server:

Address = Ep.Model.extend({
  street: Ep.attr('string'),
  city: Ep.attr('string'),
  state: Ep.attr('string')
});

// Legacy API server ships JSON like:
{ matched_addresses: [ {uglyStreet: '...', oldCity: '...', funkyState: '...'} ] };

// Shiny new API server ships what you'd expect:
{ addresses: [ {street: '...', city: '...', state: '...'} ] };

I'd like both matched_addresses and addresses to deserialize to an Address instance. To do this, I could create a custom serializer:

MatchedAddressSerializer = Ep.ModelSerializer.extend({
  // Without my PR, this gets overwritten in `serializerFor`
  typeKey: 'address',

  deserialize: function(json) {
    return this._super({
      street: json.uglyStreet,
      city: json.oldCity,
      state: json.funkyState
    });
  }
});

Right now, this isn't possible because serializerFor overwrites typeKey. I just added a check.

ghempton commented 10 years ago

Thanks for the test!

yaymukund commented 10 years ago

np, thanks for the help with the testing stuff!