Ember.js Ember data 1.0.0:与每类型适配器和序列化程序混淆
我正在从Ember data 0.13迁移到1.0.0 beta版。根据文档,现在有每类型适配器和每类型序列化器 这意味着我不能再为主键和身份验证定义具有某些特定覆盖的“myRestAdapter”。我现在需要为每个模型类型实现此代码,从而将相同的代码复制xx倍 余烬数据0.13中的代码:Ember.js Ember data 1.0.0:与每类型适配器和序列化程序混淆,ember.js,ember-data,Ember.js,Ember Data,我正在从Ember data 0.13迁移到1.0.0 beta版。根据文档,现在有每类型适配器和每类型序列化器 这意味着我不能再为主键和身份验证定义具有某些特定覆盖的“myRestAdapter”。我现在需要为每个模型类型实现此代码,从而将相同的代码复制xx倍 余烬数据0.13中的代码: App.AuthenticatedRestAdapter = DS.RESTAdapter.extend({ serializer: DS.RESTSerializer.extend({ p
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 data 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作为主键的模型复制相同的块?是否不再有一种方法可以为整个应用程序指定此块一次?既然代码接缝是不可知的,为什么不创建您的模型可以扩展的自定义序列化程序,som例如:
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();
...
希望能有所帮助。您也可以设置
App.ApplicationSerializer
。如果您希望将此规范化应用于每个模型,这将起作用
我真的不知道是否建议这样做,但因为我需要每个模型的主键都是“_id”,所以我只做了以下操作:
DS.JSONSerializer.reopen({
primaryKey: '_id'
});
我发现这与主键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切换到
属性
,并将hash
传递到super中。这可能是Ember Data 1.0 Beta版的一个bug?没有定义pertype适配器/序列化器的模型仍然回退到默认适配器/序列化器吗?@MehulKar我还没有找到回退的可能性ck到默认适配器。@直觉像素我认为您可能混淆了哈希和属性。@直觉像素规范化方法应该是函数(类型、哈希、属性)而不是函数(类型、属性、哈希)根据Ember Data 1.0 Beta版的Transition.md。@alvincrespo,是的!谢谢你的提示,更正了我的答案。我必须进行切换,因为哈希实际上是数据类型的字符串,在我的例子中,hash
返回“movie”-这就是我正在使用的模型。我的不好。我正在使用@inuitivepixel示例。但是余烬数据repo上的transition.md具有正确的顺序。
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);
}
});