1

我使用的是 Ember Data 1.0.0 beta 1。我切换到 beta 2(刚刚发布)。

似乎模型序列化程序(我在其中规范化 id)不再起作用。

我的印象是 normalize 中的参数顺序从 type、prop、hash 更改为 type、hash、prop。

这是迁移指南的建议:

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

beta 2 中的参数顺序现在是(类型、哈希、属性)。因此,标准化的模型在 beta 2 版本中不包含 id。

如果我将参数切换为 type、hash、property,则 id 会被填充,但此时所有其他属性都会变得空空如也。

因此看起来你不能再使用 normalize 来规范化 id 和任何带下划线的属性。

4

1 回答 1

1

在 Transition 文档中有一个规范化函数的修订版本(稍微更健壮)。

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

如果您只在使用normalize: function(.... 您还必须return this._super(...在线切换它。

于 2013-09-06T19:17:30.660 回答