4
votes

I'm migrating an ember application from ember-data v0.13 to v1.0.0 .

In version v0.13, RESTSerializer used to have a materialize callback that allowed me to map rails STI models to ember models.

So when I get a list of events with different types, i would convert each of them to the appropriate ember model

"events": [
    {
        "id": 1,
        "type": "cash_inflow_event",
        "time": "2012-05-31T00:00:00-03:00",
        "value": 30000
    },
    {
        "id": 2,
        "type": "asset_bought_event",
        "asset_id": 119,
        "time": "2012-08-16T00:00:00-03:00",
        "quantity": 100
    }
]

Ember models

App.Event = DS.Model.extend({...})
App.AssetBoughtEventMixin = Em.Mixin.create({...})
App.AssetBoughtEvent = App.Event.extend(App.AssetBoughtEventMixin)
App.CashInflowEventMixin = Em.Mixin.create({...})
App.CashInflowEvent = App.Event.extend(App.CashInflowEventMixin)

Ember-data code v0.13 that created the STI-like ember models

App.RESTSerializer = DS.RESTSerializer.extend({
    materialize:function (record, serialized, prematerialized) {
        var type = serialized.type;
        if (type) {
            var mixin = App.get(type.classify() + 'Mixin');
            var klass = App.get(type.classify());
            record.constructor = klass;
            record.reopen(mixin);
        }
        this._super(record, serialized, prematerialized);
    },

    rootForType:function (type) {
        if (type.__super__.constructor == DS.Model) {
            return this._super(type);
        }
        else {
            return this._super(type.__super__.constructor);
        }
    }
});

How can I do the same thing in ember-data v1.0.0 ?

2

2 Answers

2
votes

I think I've got a solution...

There's a setupData callback on models. I did the following

App.Event = DS.Model.extend({
    ...
    setupData:function (data, partial) {
        var type = data.type;
        if (type) {
            var mixin = App.get(type.classify() + 'Mixin');
            this.reopen(mixin);
        }
        delete data.type;
        this._super(data, partial);
    },
    eachAttribute: function() {
        if(this.get('type')){
          var constructor = App.get(this.get('type').classify());
          constructor.eachAttribute.apply(constructor, arguments);
        }
        this._super.apply(this, arguments);
     }
});

Ember experts, is this a good idea??

1
votes

First, since you are using Rails, you may want to use the ActiveModelAdapter & extend your custom serializer from its serializer:

App.ApplicationAdapter = DS.ActiveModelAdapter;
App.ApplicationSerializer = DS.ActiveModelSerializer.extend({...});

It looks like your custom serializer should override typeForRoot & probably normalize. Here is what those methods looks like now:

DS.ActiveModelSerializer#typeForRoot:

typeForRoot: function(root) {
  var camelized = Ember.String.camelize(root);
  return Ember.String.singularize(camelized);
}

DS.JSONSerializer#normalize:

normalize: function(type, hash) {
  if (!hash) { return hash; }

  this.applyTransforms(type, hash);
  return hash;
}