6

我正在从 Ember 数据 0.13 迁移到 1.0.0 beta。根据文档https://github.com/emberjs/data/blob/master/TRANSITION.md,现在有每个类型的适配器和每个类型的序列化器。

这意味着我不能再定义一个“myRestAdapter”,其中包含一些针对主键和身份验证的特定覆盖。我现在需要为每种模型类型实现此代码,从而导致重复 xx 次相同的代码。

Ember 数据 0.13 中的代码:

App.AuthenticatedRestAdapter = DS.RESTAdapter.extend({  

  serializer: DS.RESTSerializer.extend({
    primaryKey: function() {
      return '_id';
    }
  }),

  ajax: function (url, type, hash) {
    hash = hash || {};
    hash.headers = hash.headers || {};
    hash.headers['Authorization'] = App.Store.authToken;
    return this._super(url, type, hash);
  }
});

Ember 数据 1.0.0 中的代码(仅用于将主键设置为 _id 而不是 _id:

App.AuthorSerializer = DS.RESTSerializer.extend({

  normalize: function (type, property, hash) {
    // property will be "post" for the post and "comments" for the
    // comments (the name in the payload)

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

});

我是否正确理解我现在需要为每个需要 _id 作为主键的模型复制同一个块?是否不再有办法为整个应用程序指定一次?

4

4 回答 4

5

由于代码接缝是type不可知的,为什么您不只是创建您的模型可以扩展的自定义序列化程序,例如:

App.Serializer = DS.RESTSerializer.extend({

  normalize: function (type, hash, property) {
    // property will be "post" for the post and "comments" for the
    // comments (the name in the payload)

    // 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, json, property);
  }

});

然后App.Serializer用于所有模型:

App.AuthorSerializer = App.Serializer.extend();
App.PostSerializer = App.Serializer.extend();
...

希望能帮助到你。

于 2013-09-01T20:03:13.640 回答
3

您还可以设置App.ApplicationSerializer. 如果您希望将此规范化应用于每个模型,这将起作用。

App.ApplicationSerializer = DS.RESTSerializer.extend({
  normalize: function (type, property, hash) {
    var json = { id: hash._id };
    // ...
    return this._super(type, property, json);
  }
});
于 2013-09-04T17:49:25.333 回答
3

我真的不知道这是否被推荐,但由于我需要每个模型的主键都是“_id”,所以我这样做了:

DS.JSONSerializer.reopen({
  primaryKey: '_id'
});
于 2013-09-06T16:37:30.860 回答
0

我发现这适用于 _id 的主键 ID:

MediaUi.ApplicationSerializer = DS.RESTSerializer.extend({

  normalize: function (type, property, hash) {
    // property will be "post" for the post and "comments" for the
    // comments (the name in the payload)

    // normalize the `_id`
    var json = { id: hash._id };
    delete hash._id;

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

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

});

这里的不同之处在于我将 for 循环中的 hash 切换为property并传入hashsuper。也许这是 Ember Data 1.0 Beta 的一个错误?

于 2013-09-11T01:54:08.967 回答