我正在从 Ember 数据 0.13 迁移到 1.0.0 beta。根据文档https://github.com/emberjs/data/blob/master/TRANSITION.md 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 作为主键的模型复制同一个块?是否不再有办法为整个应用程序指定一次?