cyclomarc
cyclomarc

Reputation: 2012

Ember data 1.0.0 Beta 2: normalize no longer works

I was using Ember Data 1.0.0 beta 1. I switched to beta 2 (just released).

It seems that the model serializers (in which I normalize the id's) no longer work.

I have the impression that the param order in normalize is changed from type, prop, hash to type, hash, prop.

This is what the migration guide recommends:

normalize: function (type, property, hash) {
      // normalize the `_id`
      var json = { id: hash._id };
      delete hash._id;

      // normalize the underscored properties
      for (var prop in hash) {
        json[prop.camelize()] = hash[prop];
      }

      // delegate to any type-specific normalizations
      return this._super(type, property, json);
    }

The params order in beta 2 is now (type, hash, property). As a result, models normalized do not contain an id in the beta 2 version.

If I switch the params to type, hash, property, then the id gets filled, but all other properties become ampty at that moment.

It thus look slike you can no longer use normalize to normalize both the id and any underscored properties.

Upvotes: 1

Views: 637

Answers (1)

Jeremy Green
Jeremy Green

Reputation: 8594

There's a revised version of the normalize function (which is slightly more robust) in the Transition document.

https://github.com/emberjs/data/blob/master/TRANSITION.md#underscored-keys-_id-and-_ids

App.ApplicationSerializer = DS.RESTSerializer.extend({
  normalize: function(type, hash, property) {
    var normalized = {}, normalizedProp;

    for (var prop in hash) {
      if (prop.substr(-3) === '_id') {
        // belongsTo relationships
        normalizedProp = prop.slice(0, -3);
      } else if (prop.substr(-4) === '_ids') {
        // hasMany relationship
        normalizedProp = Ember.String.pluralize(prop.slice(0, -4));
      } else {
        // regualarAttribute
        normalizedProp = prop;
      }

      normalizedProp = Ember.String.camelize(normalizedProp);
      normalized[normalizedProp] = hash[prop];
    }

    return this._super(type, normalized, property);
  }
});

You'll get the blank params if you only switch the order on the line with normalize: function(.... You also have to switch it on the return this._super(... line.

Upvotes: 1

Related Questions