我正在从 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 作为主键的模型复制同一个块?是否不再有办法为整个应用程序指定一次?